• I’ve written a basic function that I use in my 404 page (and others) that shows my most recent posts and thought it might be of use to others.
    It’s nothing profound, but might give a few people something nice to use.

    I have removed the default styling as myself like most people tend to use their own DIVS, h1, h2, etc, and so it’s easier to just apply your DIV’s and other styling to the core functioning of the code.

    $yoursitename_posts_to_show is a numerical variable passed from where you call the function
    Change “yoursitename” throughout the function to something unique to avoid dupe var names in WordPress. I use my site name as WordPress wont have used it

    //DISPLAY LAST X POSTS
    function last_xx_posts($yoursitename_posts_to_show){
    
    if (is_numeric($yoursitename_posts_to_show)){
    
    /* gets number of recent posts limited by the number
    you assign to the variable */
    query_posts('showposts='.$yoursitename_posts_to_show); 
    
    if (have_posts()) :
    
    /*
    This shows the var (numerical) you set of number of posts to show
    If you will ever show only ONE (1) last recent post, you will need
    to add some PHP to allow for "Last 1 post" (not "postS")
    /*
    echo 'Last '.$yoursitename_posts_to_show.' most recent posts';
    
    while (have_posts()) : the_post(); ?>  
    
       <!-- display the post's comments image -->
       <img src="<?php bloginfo('template_directory'); ?>/images/commentsicon.gif" alt="Comments" />
    
       <!-- display the post's comments in numerical -->
       <?php comments_number(__('0'), __('1'), __('%')) ?>
    
       <!-- display the post title as a link -->
       <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    
       <!-- display time and date the post was created -->
       <?php the_time('l, F jS, Y'); ?>
    
       <!-- display the post's author -->
       By: <?php the_author_link(); ?>
    
       <!-- display the post/article itself and the more link (mine shows "continue reading" then the article title -->
       <?php the_content("<strong>Continue Reading</strong> - " . the_title('', '', false)."..."); ?>
    
    <?php
    endwhile; //end loop of all posts
    endif; //end if have posts
    endif; //end if variable is numeric
    }//end function
    ?>

    To use this function, copy and paste the above in your wp-content/themes/yourtheme/functions.php file
    If you don’t have this file you can just make one
    Read about that here
    https://codex.www.ads-software.com/Theme_Development#Theme_Functions_File

    Then simply use the following code in any of your template files

    <?php
    //show last 5 posts
    $yoursitename_posts_to_show = 5;
    last_xx_posts($yoursitename_posts_to_show);
    ?>

    Set the “5” to however many you want to show.

    ***IMPORTANT NOTES***

    NOTE A
    DO NOT call this function with the above code in ANY file where “The_Loop” has already been used.

    That is:

    <?php if (have_posts()) :
    while (have_posts()) : the_post(); ?>

    This includes of course index.php, archive.php, etc
    Although, I found I didn’t want to anyway as these files already have a bunch of posts, and in single.php the post comments are under the article and that tends to be enough for one page!

    Use this code somewhere such as at the bottom of your 404 and perhaps like me you have “pages” like “about this site” that usually
    don’t have any articles or posts or comments, so using this you can have the last x most recent

    NOTE B
    THIS WILL override your “Blog pages show at most” setting in Admin -> Settings -> Reading.
    So if you set 3 most on one page there, and use the function to display the last 5 posts, it WILL display the last 5 posts!

Viewing 1 replies (of 1 total)
  • Is there anyway to add this to non-blog pages?

    If not, do you know of anything that fulfills that functionality?

    Thanks in advance,

    -Clay

Viewing 1 replies (of 1 total)
  • The topic ‘Display any number of your most recent posts anywhere on your blog’ is closed to new replies.