WordPress.org

Ready to get started?Download WordPress

Forums

Pull Child category but not Parent into posts query (8 posts)

  1. onerutter
    Member
    Posted 6 years ago #

    Im trying to pull in child categories of a master category in query posts. Does anyone know how to do this?

    Im using this <?php the_category() ?>, but its pulling in both the parent and child and thats not what I want!

    Please help!

    Jake

  2. Kafkaesqui

    Posted 6 years ago #

    How are you using the_category() here exactly?

    It would help to know *where* you expect to collect the child categories from. Is this on category queries, or on single post pages? Or something altogether else?

  3. onerutter
    Member
    Posted 6 years ago #

    on category queries. Im trying to generate a list of all the children of a category and their posts.

  4. onerutter
    Member
    Posted 6 years ago #

    well basically it would look something like this:
    child-category | post title

    and it would loop through everything within the parent category - but only show the children not the parents...

    Is that do-able?

  5. Kafkaesqui

    Posted 6 years ago #

    I'm always tempted to answer that last question with:

    Of course it is. But are you up to the task? ;)

    Actually it's not that hard, since what you're asking for is a simple way to call or query any child categories, and set post loops for each of these (if I'm reading you correctly).

    For the child category 'call' we can make use of the category cache:

    <?php $categories = get_categories(); ?>

    From here you can test on whether the current category ($cat) is a parent to any others:

    <?php
    foreach( $categories as $category ) :
    	if( $cat == $category->parent ) :
    ?>
    
    ~do your stuff here~
    
    <?php endif; endforeach; ?>

    $category will hold the values you'll need for setting up your queries and such (in that 'do your stuff here' portion). Couple important properties for that:

    $category->name = Category name or title
    $category->term_id = Numeric category ID

  6. onerutter
    Member
    Posted 6 years ago #

    Yeah, Im up for the task.

    So that will pull in the child categories?

    Im confused as to what this statement means??
    if( $cat == $category->parent ) :

  7. Kafkaesqui

    Posted 6 years ago #

    if( $cat == $category->parent ) :

    $cat is a global variable that's available when on a category query. When viewing the parent category for one or more children, the if statement will evaluate as true, as it compares $cat to the parent category value for each child category ($category->parent).

  8. onerutter
    Member
    Posted 6 years ago #

    Ok so here is my code:

    <table>
    <?php
    $myposts = get_posts('category=8');
    foreach($myposts as $post) :
    ?>
       <tr>
    	<td><?php the_title(); ?></td>
    	<td> Single Category Child Goes Here</td>
    	</tr>
    <?php endforeach; ?>
    </table>

    Im looping through and I want to show the title - child category for each.

    In category 8 - I have about 20 sub-categories. Does this make sense?

Topic Closed

This topic has been closed to new replies.

About this Topic