WordPress.org

Forums

how to exclude all child categories of a category from showing up in the loop (3 posts)

  1. megaman3
    Member
    Posted 6 years ago #

    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.

  2. alysko
    Member
    Posted 6 years ago #

    Thanks a lot, it's very useful !

  3. termserv
    Member
    Posted 6 years ago #

    Thanks! Just what I was after :)

Topic Closed

This topic has been closed to new replies.

About this Topic