List all posts in selected categories
-
Hello,
Thanks for any help on this.
I want to list a category name, and then all the posts in full that are in that category.
Then, when all the posts are finished listing, I want to show another category name and all the associated posts underneath it and so on.
I want to have control over which categories get listed, by using the cat id’s or something.
I think this is easy php but I tried everything, sorry.
Thank you
James
-
Did you try to create a page of posts with multiple loops?
Thank you. Yes I was thinking that. Maybe I need a ‘for each’ loop so that:
for each category, list all posts
Is that possible?
Thanks for your time David
I almost have it:
1 <?php query_posts(‘category_id=4,3’); ?>
2 <?php while (have_posts()) : the_post(); ?>
3 <?php the_category(‘, ‘) ?>
4 <? the_title(); ?>
5 <? the_content(); ?>
6 <?php endwhile; ?>I just need to loop lines 3 to 5
This is just raw testing so please disregard the formatting.
Yes make the page of posts in one category first.
Then dupe it for a second third, fourth, like so…
<?php $my_first_category_query = new WP_Query('category_name=my_first_category'); ?> <?php while ($my_first_category_query->have_posts()) : $my_first_category_query->the_post(); ?> <!-- Do special_cat stuff... --> <?php endwhile; ?>
<?php $my_second_category_query = new WP_Query('category_name=my_second_category'); ?> <?php while ($my_second_category_query->have_posts()) : $my_second_category_query->the_post(); ?> <!-- Do special_cat stuff... --> <?php endwhile; ?>
and so on.
You don’t want to query_posts, what you where after in the original post needs a new WP_Query.
1 <?php $my_query4 = new WP_Query('cat=4'); ?> 2 <?php if( $my_query4->have_posts() ) : 3 <?php echo 'My Category 4'; ?> 4 <?php while ($my_query4->have_posts()) : $my_query4->the_post(); ?> 5 <? the_title(); ?> 6 <? the_content(); ?> 7 <?php endwhile; ?> 8 <?php endif; ?>
then
1 <?php $my_query3 = new WP_Query('cat=3'); ?> 2 <?php if( $my_query3->have_posts() ) : 3 <?php echo 'My Category 3'; ?> 4 <?php while ($my_query3->have_posts()) : $my_query3->the_post(); ?> 5 <? the_title(); ?> 6 <? the_content(); ?> 7 <?php endwhile; ?> 8 <?php endif; ?>
and so on
Oh I see, yep that works well. Thank you David. I learnt something today. Nice.
- The topic ‘List all posts in selected categories’ is closed to new replies.