0

I have 3 categories and i want to display on a loop the last 3 post of every category, or the last 4 from one category and 2 from the others etc

I can't find any solution for that and i have tried many things with no result. Can you help me?

  • when you say the last 3, can you be more specific? Categories don't have a set order, you'll have to be specific about how they're ordered, do you mean the default chronologically? AKA the 3 oldest posts in a particular category? – Tom J Nowell Oct 2 '17 at 12:17
  • yes i mean the last posts chronologically. – nnikkos Oct 2 '17 at 13:11
0

If you dont have the same post with more than 1 category, you can do:

$args = array(
    'cat' => $category->term_id,
    'post_type' => 'post',
    'posts_per_page' => '3',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
    <section class="<?php echo $category->name; ?> listing">
        <h2>Latest in <?php echo $category->name; ?>:</h2>
        <?php while ( $query->have_posts() ) {
            $query->the_post();
            ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class( 
'category-listing' ); ?>>
            <?php if ( has_post_thumbnail() ) { ?>
                <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail( 'thumbnail' ); ?>
                </a>
            <?php } ?>

            <h3 class="entry-title">
                <a href="<?php the_permalink(); ?>">
                    <?php the_title(); ?>
                </a>
            </h3>

            <?php the_excerpt( __( 'Continue Reading <span class="meta-nav">&rarr;</span>', 'twentyfourteen' ) ); ?>

        </article>

    <?php } // end while ?>

</section>

<?php } // end if

// Use reset to restore original query.
wp_reset_postdata();
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.