0

I am using this string to post a list of categories:

<?php single_cat_title('You are currently viewing posts we\'ve published under the tag: '); ?>.

What I would like to do is embed posts from a specific category on a page.

And, if possible, to limit those posts to the most recent 20.

Is that possible?

I think there are plugins that can do that but I'd rather just use a line of code.

Thanks

  • have you tried get_posts? – inarilo Jan 22 '18 at 1:03
0

That's certainly possible. Use WP_Query to search for posts, then output them as you please.

For example, to get 20 posts from the category with the id 4:

<?php
$query = new WP_Query(
    array(
        'category__in' => 4
        'posts_per_page' => 20
    )
);

if ( $query->have_posts() ) {
    echo "Here are some posts from that category:";
    echo '<ul>';
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
    }
    echo '</ul>';
    wp_reset_postdata();
}

To query other post types or taxonomies, look at the documentation, it has lots of examples.

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.