How Do I Create a Shortcode for Recent Post in WordPress?

Creating a shortcode for recent posts in WordPress is very simple. Simply create a new file called “recent-posts.php” and add the following code:

$post

“; } } else { echo “

No recent posts found.

“; } ?>

The first line sets up the global variables we will need. The wp_get_recent_posts() function will return a list of recent posts.

If the function is successful, the output will be an array; otherwise, the output will be a plain paragraph.

The next line loops through the array of posts and prints the title and excerpt for each one.