The following code lists the last whatever posts in all categories and uses the current theme for formatting the summary.
<?php /* Start the Loop */ ?>
<div class="posts-loop">
<?php
while (have_posts()) {
the_post();
?>
<?php
/* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part('template-parts/' . $post_template);
?>
<?php
}
?>
</div><!-- / .posts-loop -->
What I would like is to list posts by category but keep the formatting, so say I have the following categories:
So what I envision is:
Category 1
[formatted summary of category 1 post 1 based on theme]
[formatted summary of category 2 post 1 based on theme]
[formatted summary of category 3 post 1 based on theme]
Category 2
[formatted summary of category 1 post 1 based on theme]
[formatted summary of category 2 post 1 based on theme]
[formatted summary of category 3 post 1 based on theme]
etc.
There is an example at Forward Progressives that shows what I am trying to get. My site lists nicely for all posts.
I tried to modify the PHP code and tried a few plugins but nothing seems to work.
I tried the following PHP code, but that did not get me very far.
$args = array('numberposts' => 10, 'category' => 'features');
$latestPosts = get_posts($args);
if ($latestPosts)
{
foreach ($latestPosts as $post)
{
setup_postdata($post); ?>
/* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part('template-parts/' . $post_template);
}
wp_reset_postdata();
}
Thoughts?
</div>
Should be easy enough with get_categories()
, WP_Query()
and a couple loops.
// Get all your categories
$categories = get_categories( array(
'parent' => 0, /* we only want parent categories */
'orderby' => 'name', /* order by category name, ascending */
'order' => 'ASC'
) );
// Loop through each one
foreach( $categories as $category ) {
?><h2><?php echo( $category->name ); ?></h2><?php
// Get some posts from each one
$query = new WP_Query( array( 'category_name' => $category->name ) );
// Do all the stuff your theme already does
?>
<div class="posts-loop">
<?php
while ($query->have_posts()) {
$query->the_post();
?>
<?php
/* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part('template-parts/' . $post_template);
?>
<?php
}
?>
</div><!-- / .posts-loop -->
<?php
// Reset query and loop again to next category, if there is one
wp_reset_query();
}