WordPress Development Tutorial: Creating WordPress ShortCode To Display Recent Posts


The WordPress 2.5 version has brought WordPress shortcode API into the spotlight. In today’s WordPress community, nine out of ten people prefer to use WP shortcode when it comes to WordPress development. The reason??? It is a simple set of functions most commonly used for creating macro codes. Shortcodes come with great flexibility and allows every average user to publish dynamic content using macros without the wisdom of expert programming skills.

Despite of easiness and awesomeness of WordPress CMS, many people find it problematic especially when it comes to displaying the most recent posts on a page or page. If you are also facing the same issues then scroll down to read a quick tutorial. This tutorial provides you a step-by-step process of everything right from creating shortcode for [recent-posts] to [recent-posts posts = “5”] and [recent-posts posts = “5”] Posts Heading [/recent-posts]. Let’s get started:

How to Create Shortcode to Display Recent Posts

In the first part, we will be creating the WordPress shortcode for new posts. Interestingly, the overall creation process hardly requires any PHP knowledge and skills.

1) Create the function, which is the callback function.

2) Register the shortcode with a unique name to avoid conflicts.

3) Hook the code into WP to execute register_shortcodes () function. For this, you need to add the registration function to WordPress’ initialization action.

How to Create Shortcode to Display Posts in a Particular Set of Number

To display a certain number of recent posts, you need to create an extra set of shortcode. For this, you have to use WordPress’ built-in shortcode_atts () function and extract () PHP function. WordPress’ built-in shortcode_atts () function plays a significant role in combining the parameters (attributes) of user shortcode with native parameters (attributes). On the other hand, the second function helps to extract the shortcode attributes.

How to Create Shortcode to Display a Heading for List of Recent Posts

To add the ability to display Heading as a content for the list of latest posts, you need to take an advantage of the second parameter, which is $content in the callback function. Further, add it as a h3 heading. It is somehow similar to an HTML tag.

Don’t forget to test all your shortcode. It is a very important and necessary step. It ensures whether the codes are working properly or not. If you are an amateur coder with minimum coding knowledge, then you should refer shortcode WordPress Plugins to style posts and pages. Besides that, you can also hire professional assistance of a reliable PSD to WordPress company on full-time, part-time or hourly basis.


Source by William B Johnson

Leave a Reply