S
S
shane892015-10-14 14:45:34
WordPress
shane89, 2015-10-14 14:45:34

How to display posts only child categories in wordpress?

Good evening. When switching to a parent category, its child subcategories are displayed, but posts of child categories are also displayed. How to display posts only in child categories, and remove them in the parent category. is it empty?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Foo Bar, 2015-10-16
@atomheart

Approximately it is done like this:
Here https://codex.wordpress.org/The_Loop in the section "Multiple Loops Example 1" there is an example of solving your case:

// Get the last 10 posts in the special_cat category.
<?php query_posts( 'category_name=special_cat&posts_per_page=10' ); ?>

<?php while ( have_posts() ) : the_post(); ?>
  <!-- Do special_cat stuff... -->
<?php endwhile; ?>

Because If your parent is empty, then nothing will be displayed.
Just in case, this is how you can exclude categories from the list, but there are suspicions that it will exclude subdirectories:
<?php
if ( is_home() ) {
  query_posts( 'cat=-1,-2,-3' );
}
?>

As a last resort, you can manually track the category of the current post in a loop and skip posts from the parent category. This is how you can find out the current category (parent):
Read more about query_posts here https://codex.wordpress.org/Function_Reference/que...
I hope this helps you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question