[Support request] Create ‘recent posts’ page similar to author, category and tag pages

Home Forums Support [Support request] Create ‘recent posts’ page similar to author, category and tag pages

Home Forums Support Create ‘recent posts’ page similar to author, category and tag pages

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1653196
    Zee

    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!

    #1653389
    David
    Staff
    Customer Support

    Hi there,

    how does this differ from the standard Category Archives ?

    #1653512
    Zee

    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.

    #1653794
    Elvin
    Staff
    Customer Support

    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/

    A wise man once said:
    "Have you cleared your cache?"

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.