WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] categories and subcategories loop in a page template (6 posts)

  1. ArielZusya
    Member
    Posted 2 years ago #

    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.

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    See a page of posts for some ideas.

  3. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

  4. ArielZusya
    Member
    Posted 2 years ago #

    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

  5. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    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 );
  6. ArielZusya
    Member
    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic