Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been trying to create a loop that will only show the top level posts no children/sub category posts.

Basically what my categories look like are --

-All

--one
--- A
--- B

-- two
--- A
--- B

-- three
--- A --- B

So I need one, two, and three to show on 'All' category page - but none of a & b. I've checked other questions on here, and search google but none of the methods seem to work. The only one I found to get close was this loop --

<?php 
// 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 = "category__in=$catID&order=ASC&showposts=5";
     }              


        $result = new WP_Query($verticalNavigationSwitcher);
        ?>

<?php if ( $result->have_posts() ) : ?>

  <!-- pagination here -->

  <!-- the loop -->
  <?php while ( $result->have_posts() ) : $result->the_post(); ?>

// some divs

 <?php endwhile; ?>
  <!-- end of the loop -->

  <!-- pagination here -->

  <?php wp_reset_postdata(); ?>

<?php else:  ?>
  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?> 

The only problem with that is it's only showing one post instead of showing all the top level posts.

So how can i fix this to show all the current category immediate/top level posts?

share|improve this question
    
Is the "'All' category page" a WordPress "Page" or is this a template like category.php? –  s_ha_dum Apr 14 at 14:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.