Calling WP_Query within a foreach loop
-
Hi guys,
I have been struggling to figure this out for a few hours now and cannot get anywhere.Basically, what I want this code to do is to list all of the categories and posts within them with content separated by categories.
Here is my code:
$categories=get_categories('orderby=name'); foreach ($categories as $cat) { $the_query = new WP_Query('category_name='.$cat->cat_name); while ($the_query->have_posts()) : $the_query->the_post(); echo "<div class=\"post\">"; echo "<b>"; echo "<a href=\""; the_permalink(); echo "\">"; the_title(); echo "</a>"; echo "—"; the_time('M j, Y'); echo "</b>"; echo "<div class=\"content\">"; the_content('—'); echo "</div>"; echo "</div>"; endwhile; }
The script is working (somewhat). It lists the categories, but only displays the first post in each category. I want all of them to be displayed. Also, my blog posts per page is set to 50.
I haven’t really used WP_Query before (this is probably apparent to those who have), so any help would be greatly appreciated.
Thanks,
Greg
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Calling WP_Query within a foreach loop’ is closed to new replies.