Support » Fixing WordPress » how to exclude all child categories of a category from showing up in the loop

  • I came across the need to exclude all child categories of a single category from showing up in the loop on the home page of my blog.

    The solution was to place this code right before the loop:

    if ( is_home() ) {
    	$child_cats = (array) get_term_children('14', 'category');
    	query_posts(array('category__not_in' => array_merge(array('14'), $child_cats)));
    }

    (14 being the parent category of the child categories to be excluded)

    For example:

    <?php
    
    	if ( is_home() ) {
    	$child_cats = (array) get_term_children('14', 'category');
    	query_posts(array('category__not_in' => array_merge(array('14'), $child_cats)));
    }
    
    	while (have_posts()) : the_post(); $postcount++; ?>

    Thanks to sivel in #wordpress irc for the code. I’m sharing it here because I could not find a solution to this anywhere in support.

    Keywords: exclude / hide all child categories of a single category from the loop / from appearing on the home page.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘how to exclude all child categories of a category from showing up in the loop’ is closed to new replies.