I suggest you use a plugin with shortcode capability for maximum control.
Create a file named “my-recent-posts.php” inside wp-content/plugins/ and paste the following code in it.
<?php
/*
Plugin Name: My Recent Posts
Description: Display recent posts with the [recent_posts] shortcode
*/
function display_recent_posts($attr)
{ $catID = ( isset($attr['category']) ? get_cat_ID($attr['category']) : 0 );
$no = ( (isset($attr['number']) && intval($attr['number'])) ? $attr['number'] : 10 );
$posts = wp_get_recent_posts( array('numberposts' => $no , 'category' => $catID ) );
echo '<ul>';
foreach($posts as $post)
echo '<li><a href="' . get_permalink($post["ID"]) . '" >' . $post["post_title"].'</a> </li>';
echo '</ul>';
}
add_shortcode('recent_posts', 'display_recent_posts');
The closing ?>
tag has been deliberately omitted.
Activate this plugin from the plugins panel, edit your portfolio and use any of the following shortcodes.
[recent_posts]
– Displays 10 recent posts.
[recent_posts number="3"]
– Displays 3 recent posts.
[recent_posts number="2" category="Uncategorized"]
– Displays 3 recent posts from the Uncategorized category.
You may use this shortcode on any post or page.