Grow your CSS skills. Land your dream job.

Last updated on:

Recent Posts Function

Technique #1

This function is useful when you need to display content, excerpt, custom fields, or anything related to the post beyond it's link and title. If you just need a list of linked titles, see the next technique. Put the following function in functions.php

function recent_posts($no_posts = 10, $excerpts = true) {

   global $wpdb;

   $request = "SELECT ID, post_title, post_excerpt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' ORDER BY post_date DESC LIMIT $no_posts";

   $posts = $wpdb->get_results($request);

   if($posts) {

               foreach ($posts as $posts) {
                       $post_title = stripslashes($posts->post_title);
                       $permalink = get_permalink($posts->ID);

                       $output .= '<li><h2><a href="' . $permalink . '" rel="bookmark" title="Permanent Link: ' . htmlspecialchars($post_title, ENT_COMPAT) . '">' . htmlspecialchars($post_title) . '</a></h2>';

                       if($excerpts) {
                               $output.= '<br />' . stripslashes($posts->post_excerpt);
                       }

                       $output .= '</li>';
               }

       } else {
               $output .= '<li>No posts found</li>';
       }

   echo $output;
}

Usage

After you've made the function. Put the following in the sidebar or wherever you like the recent posts to list..

<?php recent_posts(); ?>

You can give it 2 arguments, the first is the number of posts and the second is whether or not you want to display the excerpts. so recent_posts(2, false) will display the 2 most recent post titles.

Technique #2

<?php wp_get_archives( array(

    'type'            => 'postbypost',   // or daily, weekly, monthly, yearly
    'limit'           => 10,   // maximum number shown
    'format'          => 'html',   // or select (dropdown), link, or custom (then need to also pass before and after params for custom tags
    'show_post_count' => false,    // show number of posts per link
    'echo'            => 1     // display results or return array

) ); ?> 

Technique #3

More succinct version of #1, which also includes a more standardized query string.

<?php
   $recentposts = get_posts('numberposts=12&category=4');
   foreach ($recentposts as $post) :
       setup_postdata($post); ?>
       <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>

Reference URL

Comments

  1. Permalink to comment#

    when i upgrade wordpress one version to another new version…

    is it the function.php is overwritten by updates..? and need to re-add the above code..??

    Please advice…

  2. aki
    Permalink to comment#

    mans! weh dah dah la tu dow ng website ko.haha..

    @chris, thank for this man

  3. jmezi
    Permalink to comment#

    quick question – how do you add the date to the function?

  4. mauro
    Permalink to comment#

    thanks

  5. @mansur, no it is not, the functions.php file is a part of your theme which is not updated during a wordpress update, it is only updated when u update your theme.

    The functions.php file will only be updated during a wordpress update if you’re using the default wordpress theme even if you may have customized it.

    If you’re currently using the default theme, the solution is renaming the theme from the style.css file, you should also rename the theme folder(very important!).

  6. Permalink to comment#

    I am a newbie … and I would like to know how you even determine when to implement the above post … cause it sounds cool … but how do you know “WHEN”

  7. Ok I have a crazy question. A client of mine would like to display the latest 3 post on their home page. How would I go about breaking down each post into a seperate div for styling?

    The format would look something like below:

    Show Recent Post / Show Second Recent Post / Show Third Recent Post

    Each of the above are in separate DIV’s Hope that somewhat helps…

    Thanks in Advance,

    Aaron

  8. its not working for my blog :(

  9. Frank Waive
    Permalink to comment#

    @interviewdate post a sample of the code you implemented, maybe i could help (that’s if you haven’t figured it out already)

  10. benbelli
    Permalink to comment#

    please help me … I want to show recent posts in my index.php …. not in woordpress

    and this is the code i used i to input the posts

    but i didn’t know ho to get just the recent posts for eg the 8 recent posts…..

    php

    $re= “select * from tableaux

    LIMIT 0 , 7″;

    $k = mysql_query($re);

    while($s=mysql_fetch_row($k))
    {

    $t = substr($s[2] , 0 , 300 );
    $t=$t.” …”;
    echo ‘
    a href=”‘.$s[3].’”title=”‘.$s[1].’” ></a

    }

  11. dahans
    Permalink to comment#

    Works great. But how can i show the “Post Date” ???

  12. matka złodzieja
    Permalink to comment#

    where I should put the code <?php recent_posts(); ?> ?

  13. Macdara
    Permalink to comment#

    Thanks very much these tutorials and loops are great

  14. Adam
    Permalink to comment#

    To include the date just do this:

    In the function replace this
    $request = "SELECT ID, post_title, post_excerpt FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_type='post' ORDER BY post_date DESC LIMIT $no_posts";
    with this

    $request = "SELECT ID, post_title, post_excerpt, post_date FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_type='post' ORDER BY post_date DESC LIMIT $no_posts";

    and just add it to your output a bit like this
    $output .= '<a href="' . $permalink . '" title="Permanent Link: ' . htmlspecialchars($post_title, ENT_COMPAT) . '" rel="nofollow">' . htmlspecialchars($post_title) . '(' . htmlspecialchars($post_date) . ')' . '</a>';

    • Adam
      Permalink to comment#

      Or to format your date in the format of 24th April 2014 change post_date in the query to DATE_FORMAT( post_date, '%D %M %Y' ) AS post_date

Leave a Comment

Current day month ye@r *

*May or may not contain any actual "CSS" or "Tricks".