Add Recent Posts to your WordPress page without using a plugin

If you have a WordPress site and you would like to know how to display some of your recent posts on a page or in a template without using plugins then read on.

Should I use plugins?

First though, a little discussion on using plugins in general. Plugins are great, they allow us to extend our websites in an almost limitless fashion, all within a few clicks. We don’t need to spend a lot of time and/or money building functionality that already exists. A lot of care and thought has gone in to some plugins and you wouldn’t want to release a website without Yoast for example.

But don’t fall in to the trap of installing a plugin for everything you want to do! Do you really need a plugin to add you Google Analytics tracking code?

There are numerous reasons why you should avoid using plugins on your WordPress site where possible. Firstly, they do pose a security risk, especially when downloaded from a less than reliable source. If you do want to use a plugin try to make sure it is from a reliable source and that it is updated regularly when vulnerabilities are found. The last thing you want is a hacked WordPress site.

Plugins also need updating regularly, can cause conflicts with each other and a lot of the time add ‘bloat’ to your site. This often means they will slow your website down unnecessarily. This is frustrating for your users and may effect your rankings in the search results.

Where possible, avoid using plugins and if you can, build your own functionality. The added bonus of custom built code is that you can make it do exactly what you want it to do and you won’t be limited by the configuration settings of a plugin.

How to display your recent blog posts

When it comes to displaying your recent posts on a page or in a template, do you really need to install a plugin? No. It is relatively simple to build this yourself and you can then use your shortcode wherever you like, either directly in the editor or in a page template.

At this point I’m going to assume that you are familiar with editing and writing code, or can at least understand it! Also, that you can access your website files via FTP and edit them with a code editor of your choice. If you can’t, then it’s probably best you ask a developer to do this for you. Or, of course get in touch and we will happily discuss your project with you.

For this example I am working on a site using Bootstrap 4, you can of course modify the code to style it however you like. The important thing here is how simple it is to create your own shortcode that will quickly return your recent posts thumbnail, title, excerpt and give the option for the user to click through and read the full blog post.

In your functions.php file add the code below (or as we prefer to do, call a specific shortcodes.php file by adding ‘include(‘shortcodes.php’); in your functions.php).


function shortcode_RecentBlogPosts()
 {
    global $post;
    $html = "<section class=\"recent-blog-posts\"><div class=\"container\"><div class=\"row\">";

    $my_query = new WP_Query( array(
       'post_type' => 'post',
       'posts_per_page' => 3
    ));

    if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();

        $html .= "<div class=\"col-md-4\">";
        $html .= "<img src=" . get_the_post_thumbnail_url($post_id) . ">";
        $html .= "<h2>" . get_the_title() . " </h2>";
        $html .= "<p>" . get_the_excerpt() . "</p>";
        $html .= "<a href=\"" . get_permalink() . "\" class=\"btn btn-primary\">Read more</a>";
        $html .= "</div>";

    endwhile;
    
    $html .= "</div></div></section>";

    wp_reset_postdata();
    endif;

    return $html;
 }
 
 add_shortcode( 'recentblogposts', 'shortcode_RecentBlogPosts' );

To use your newly created shortcode in your editor, on any page you want to display recent posts on, you can simply type [recentblogposts].

If you would like to automatically display them on your page using a template then just use the do_shortcode function as shown below:

<?php echo do_shortcode("[recentblogposts]"); ?>

There we have it, a few lines of customisable code that quickly fulfils the requirement to display the latest 3 recent blog posts. Because we have not used a plugin there is no need to update it, no conflicts with other plugins and no security vulnerability. It does exactly what we want it to do in minimal code.

If you would like to discuss WordPress plugins or have a specific requirement for your WordPress site then we would love to hear from you, please get in touch.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *