Site logo

Archived topic

Create 'recent posts' page similar to author, category and tag pages

3 replies · Started by Zee on February 10, 2021

Viewing posts 1–4 of 4

I'm trying to figure out whether there is a way to generate a 'recent posts' feed similar to the archive pages generated automatically by Wordpress when you click on any author, category and tag, without using a plugin (even one as awesome as WP Showposts). I don't need any customisation other than the ability to generate a similar feed by category. Have included some example URLs - hope there is a solution!

Hi there,

how does this differ from the standard Category Archives ?

Thanks for following up!

I would like to be able to put it in another page of my choice (where I can choose the URL, e.g. website.com/beans rather than website.com/category/beans), and also put things like sidebars.

Hi there,

You'll have to write something like this to make a loop query for your recent post:
https://wordpress.stackexchange.com/posts/35612/revisions

The same code transformed as a shortcode [recent_post]

add_shortcode( 'recent_post', function() {?>
<div id="posts">

<?php

    // define query arguments
    $args = array(
        'posts_per_page' => 5, // your 'x' goes here
        'nopaging' => true
        // possibly more arguments here
    );

    // set up new query
    $tyler_query = new WP_Query( $args );

    // loop through found posts
    while ( $tyler_query->have_posts() ) : $tyler_query->the_post();
        echo '<section class="post">'.
             '<h2><a href="'.
             get_permalink().
             '">'.
             get_the_title().
             '</a></h2><p>'.
             get_the_excerpt().
             '</p></section>';
    endwhile;

    // reset post data
    wp_reset_postdata();

?>

</div>
<?php } );

This is a rough sample. You can refine it to your preference.

Check the parameters for the $args here: https://developer.wordpress.org/reference/classes/wp_query/

This archived topic is closed to new replies.