• I’m trying to list my posts by subcategory groups on each category page.

    I’ve added the following code to category.php and it works well. What I need help with is the automatic detection of the child_of ID (shown as 3 at the moment) – which would be the parent category ID.

    Can anyone help? Thanks.

    <?php
    $categories = get_the_category();
    $categories =  get_categories('child_of=3');
    foreach  ($categories as $category) {
    echo '<h2>'.$category->name.'</h2>';
    foreach (get_posts('cat='.$category->term_id) as $post) {
    setup_postdata( $post );
    echo '<a href="'.get_permalink($post->ID).'">' . get_the_title() . '</a>';
    }
    }
    ?>
Viewing 1 replies (of 1 total)
  • Not sure, but this may be what you want:

    $child_of = get_query_var( 'cat' );
    $categories =  get_categories( "child_of=$child_of" );

    It is not clear what you want to happen if the category has no children or there are no posts assigned to one of the children.

Viewing 1 replies (of 1 total)
  • The topic ‘WordPress show posts in subcategory groups’ is closed to new replies.