WordPress.org

Support

Support » Themes and Templates » [Resolved] categories and subcategories loop in a page template

[Resolved] categories and subcategories loop in a page template

  • This is probably a simple task but I’m struggling and could use a hand. I’ve got a post category with several subcategories. Posts will not appear in the main category but will appear in the subcategories. I’d like to dynamically generate, on a page, the title of each sub category followed by the posts in that sub category. I don’t know if I need to use get_the_category or wp_list_cats or something else. I’m also getting all twisted in my loop. Any help would be greatly appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • esmi

    @esmi

    Forum Moderator

    See a page of posts for some ideas.

    Michael

    @alchymyth

    Forum Moderator

    Thanks for your responses. I took a look at both but I’m unfortunately still struggling to implementation. Here’s where I am… I am able to generate a page of posts that displays the name of the subcategories but and below each it shows a posts but the posts it shows do not correspond to the category name. I thought by placing my query_posts() in my foreach(): loop it would inherit the particular category but no such luck. Here’s the first attempt I made:

    <?php  $cats = get_categories('orderby=id&order=DESC&child_of=5'); 
    
    			foreach ($cats as $cat) :
    				echo '<h1>' . $cat->name . '</h1>';
    				query_posts( 'orderby=date&order=ASC' );
    				    if (have_posts()) : while (have_posts()) : the_post(); ?>
    					<?php the_content(); ?>
    				    <?php endwhile; endif; wp_reset_query(); ?>
    
    			<?php endforeach; ?>

    I then thought I needed to drop $cat into the query_posts() args but I got an error doing that. Am I on the right track? What am I doing wrong? Thanks again for you help.

    Ariel

    Michael

    @alchymyth

    Forum Moderator

    I then thought I needed to drop $cat into the query_posts() args but I got an error doing that. Am I on the right track?

    yes – the error came possibly from some syntax violation;

    instead:

    query_posts( 'orderby=date&order=ASC' );

    try:

    query_posts( 'orderby=date&order=ASC&cat='.$cat->term_id );

    you also might need to add a ‘posts_per_page’ parameter:

    query_posts( 'posts_per_page=10&orderby=date&order=ASC&cat='.$cat->term_id );

    Aha! I think that solved it. Thanks for all the help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] categories and subcategories loop in a page template’ is closed to new replies.
Skip to toolbar