• Resolved nicksoper

    (@nicksoper)


    Hi There,
    I’d like have an FAQ’s category on my site, and on the FAQ archives pages (descendant category pages of FAQ’s), to list by category and the posts insude the category.

    Eg when on the main FAQ’s category page I’d like the loop to do this.

    • FAQ Category 1
    • Post 1 Title
    • Post 2 Title
    • Post 3 Title
    • FAQ Category 2
    • Post 1 Title
    • Post 2 Title
    • Post 3 Title
    • FAQ Category 3
    • Post 1 Title
    • Post 2 Title
    • Post 3 Title

    There are a few things that do similar jobs, like Latest Post from each Category plugin for WordPress by Dagon, but it’s not quite right.

    I could manually code up multiple loops, per sub category of FAQ’s, but that would mean every time I added another sub category (which would not be often) I would need to add another loop. Not to mention the performance implications of that.

    Cheers

Viewing 2 replies - 1 through 2 (of 2 total)
  • MichaelH

    (@michaelh)

    Thread Starter nicksoper

    (@nicksoper)

    I actually figured this one out, but when I tried to post the solution I got the BB Press error page.

    Here is my solution if anyone wants to check it out. You could change “child_of=7” or remove it all together to suit your needs.

    <? 
    
    /* FIRST LOOP THROUGH THE CATEGORIES */			
    
    $categories=get_categories('child_of=7'); //outputs an array of child categories of category 7
    
    foreach($categories as $category) { ?>
    
    <?php $current_cat = $category->term_id; ?>
    
    <h2><a href="<?php echo get_category_link($current_cat); ?>" title="<?php sprintf( __( "View all posts in %s" ), $category->name ) ?>"><?php echo $category->name ?></a></h2>
    
    <?php 
    
    /* SECOND LOOP THROUGH THE POSTS IN THE CATEGORY WHILST IN THE FIRST LOOP */ 					
    
    $cat_post = new WP_Query('cat='.$current_cat); ?>
    
    <ul>
    <?php
    while ($cat_post->have_posts()) : $cat_post->the_post();
    $do_not_duplicate = $post->ID;
    $preview = get_post_meta($post->ID, 'preview', true);
    ?>
    
    <li><a href="<?php the_permalink(); ?>" title="Peralink to FAQ: <?php the_title(); ?>"><?php the_title(); ?></a></li>
    
    <?php endwhile; ?>
    
    </ul>
    
    <?php  } ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Listing Posts by Category’ is closed to new replies.