How to easily Show Recent Posts as a Drop Down in WordPress Website

Do you want to show recent posts as a drop Down in WordPress Website. So users don’t worry as in this article we are going to describe the best way for showing recent posts as a Drop Down in WordPress Website.

Why and Who Requires Recent Posts in Drop Down?

WordPress usually comes with a built-in recent posts widget that you can add to any sidebar. This widget generally shows a list of recent posts, and you can select the number of posts you want to show. But if you want to display more than 5-10 posts, then the list will take a lot of space in your sidebar.

Most of the users of WordPress need a compact way to show recent posts. In that case, using drop downs lists can help you in saving space.

Let’s see the different ways to show recent posts as a drop down menu in WordPress.

Displaying Recent Posts in a Plain Drop Down Menu

For displaying WordPress Recent Posts just copy and paste the below given code in your theme’s functions.php file.

function wpb_recentposts_dropdown() {

$string .= ‘<select id=”rpdropdown”>

<option  value=”” selected>Select a Post<option>’;

$args = array( ‘numberposts’ => ‘5’, ‘post_status’ => ‘publish’ );

$recent_posts = wp_get_recent_posts($args);

foreach( $recent_posts as $recent ){

$string .= ‘<option value=”‘ . get_permalink($recent[“ID”]) . ‘”>’ .   $recent[“post_title”].'</option> ‘;

}

$string .= ‘</select>

<script type=”text/javascript”> var urlmenu = document.getElementById( “rpdropdown” ); urlmenu.onchange = function() {

window.open( this.options[ this.selectedIndex ].value, “_self” );

};

</script>’;

return $string;

}

add_shortcode(‘rp_dropdown’, ‘wpb_recentposts_dropdown’);

add_filter(‘widget_text’,’do_shortcode’);

 

You can use the [rp_dropdown] shortcode in WordPress pages, post and text widgets.

Adding Collapsible Recent Posts Using Plugin

The above method lists your recent posts in a drop down form. The another way to save space is by adding a collapsible list of recent posts which expands when users click on it.

  • First of all install and activate the Collapse-O-Matic plugin. Take a look at how to install a Plugin in WordPress. Generally it works out of the box and there are no settings for you to configure.
  • The plugin lets you to display anything in a collapsible menu via a shortcode.

Before using the plugin, we need a way to easily show recent posts anywhere we want.

  • Simply add this code to your theme’s functions.php file or a site-specific plugin.
function wpb_recentposts() {

 

$string .= ‘<ul>’;

$args = array( ‘numberposts’ => ‘5’, ‘post_status’ => ‘publish’ );

$recent_posts = wp_get_recent_posts($args);

foreach( $recent_posts as $recent ){

$string .= ‘<li><a href=”‘ . get_permalink($recent[“ID”]) . ‘”>’ .   $recent[“post_title”].'</a></li> ‘;

}

$string .= ‘</ul>’;

return $string;

}

add_shortcode(‘recentposts’, ‘wpb_recentposts’);

add_filter(‘widget_text’,’do_shortcode’);

 

 

This code simply lets you to show a list of recent posts using the shortcode [recentposts].

  • Now we will add our shortcode in the Collapse-O-Matic shortcode for creating a collapsible list of recent posts.
  • Just Add the shortcode for example: [expand title=”Recent Posts”][recentposts][/expand]
  • You can also add this to text widget, pages and posts on your WordPress Website.

That’s all in our behalf we hope that this article helped you completely in displaying Recent Posts as a Drop Down in WordPress Website.

NameE-mailWebsiteComment

Leave a Reply

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