0
votes

I have been struggling to extract the parent category posts from Wordpress. When i am trying to display the only parent category posts the wordpress query displaying the sub-categories posts as well.

How can omit the sub-category posts. please help!!..

<?
// Get the post ID
        $id = get_the_ID();         


        //get all the categories ( this function will return all categories for a post in an array)
        $category= get_the_category( $id );

        if ($category->category_parent == 0) {

        //extract the first category from the array
        $catID = $category[0]->cat_ID;

        //Assign the category ID into the query
        $verticalNavigationSwitcher = "cat=$catID&orderby=ID&order=ASC";
     }              


        $result = new WP_Query($verticalNavigationSwitcher);


                    //$featuredPosts->query('showposts=5&cat=3');
                    while ($result->have_posts()) : $result->the_post(); 
        ?>


   <li><a href='<?php the_permalink() ?>'><span><?php the_title(); ?></span></a></li>


  <?php 
            endwhile; 
            wp_reset_postdata();
 ?> 
1
You really shouldn't post outcommented code in your question. That, and fix your code indentation (I'd be glad to, but not when it's hard to tell what is what).timss
what the wrong with the code? it will display the all the posts of the parent categories and subcategories..it is valid for every word-press installed.Anam
Outcommented code and code indentation, not what your code actually does.timss

1 Answers

0
votes

Try using this query:

$verticalNavigationSwitcher = "category__in=$catID&orderby=ID&order=ASC";