BlogThemes

How to Show Recent Posts as a Drop Down in WordPress

You’re most likely accustomed to the Class widget in WordPress. Just lately, one in every of our readers requested us if it was doable to show latest posts in a drop down as properly. On this article, we’ll present you tips on how to present latest posts as a drop down in WordPress.

Add recent posts as drop down menu

Why and Who Wants Latest Posts in Drop Down?

WordPress comes with a built-in latest posts widget that you may add to any sidebar or widget prepared space.

This widget merely shows an inventory of latest posts, and you may select the variety of posts you wish to present. However if you wish to present greater than 5-10 posts, then the record will take lots of area in your sidebar.

Some WordPress customers might have a compact approach to show latest posts. In that case, utilizing drop downs or collapsible lists can assist you save area.

Let’s check out couple of various methods to indicate latest posts as a drop down menu in WordPress.

Displaying WordPress Latest Posts in a Plain Drop Down Menu (Guide Code)

This methodology makes use of the built-in wp_get_recent_posts operate. All you have to do is copy and paste the next code in your theme’s functions.php file or a site-specific plugin.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
operate wpb_recentposts_dropdown() {
$string .= '<choose id="rpdropdown">
            <choice  worth="" chosen>Choose a Submit<choice>';
$args = array( 'numberposts' => '5', 'post_status' => 'publish' );
$recent_posts = wp_get_recent_posts($args);
    foreach( $recent_posts as $latest ){
        $string .= '<choice worth="' . get_permalink($latest["ID"]) . '">' .   $latest["post_title"].'</choice> ';
    }
$string .= '</choose>
            <script sort="textual content/javascript"> var urlmenu = doc.getElementById( "rpdropdown" ); urlmenu.onchange = operate() {
            window.open( this.choices[ this.selectedIndex ].worth, "_self" );
            };
            </script>';
return $string;
}
add_shortcode('rp_dropdown', 'wpb_recentposts_dropdown');
add_filter('widget_text','do_shortcode');

Now you should utilize the shortcode [rp_dropdown] in your WordPress put up, pages, and textual content widgets. It’ll appear like this:

Recent posts in a drop down menu on a WordPress site

Including Collapsible Latest Posts Utilizing Plugin

The above methodology merely lists your latest posts in a drop down type. One other approach to save area is by including a collapsible record of latest posts which expands when customers click on on it.

Very first thing you have to do is set up and activate the Collapse-O-Matic plugin. It really works out of the field, and there are not any settings so that you can configure.

The plugin merely permits you to present something in a collapsible menu utilizing a shortcode.

Earlier than we use this plugin, we want a approach to simply present latest posts anyplace we would like. Merely add this code to your theme’s functions.php file or a site-specific plugin.

1
2
3
4
5
6
7
8
9
10
11
12
13
operate wpb_recentposts() {
$string .= '<ul>';
$args = array( 'numberposts' => '5', 'post_status' => 'publish' );
$recent_posts = wp_get_recent_posts($args);
    foreach( $recent_posts as $latest ){
        $string .= '<li><a href="' . get_permalink($latest["ID"]) . '">' .   $latest["post_title"].'</a></li> ';
    }
$string .= '</ul>';
return $string;
}
add_shortcode('recentposts', 'wpb_recentposts');
add_filter('widget_text','do_shortcode');

This code merely permits you to show an inventory of latest posts utilizing the shortcode [recentposts].

Now we’ll add our shortcode within the Collapse-O-Matic shortcode to create a collapsible record of latest posts.

Merely add the shortcode like this:

[expand title="Recent Posts"][recentposts][/expand]

You’ll be able to add this shortcode in a textual content widget, posts, or pages in your WordPress website. That is the way it seemed on our check website.

Collapsable list of recent posts

That’s all, we hope this text helped you present latest posts as drop down in WordPress. You might also wish to see these 6 tricks to create a killer editorial calendar in WordPress.

Download The WPSaviour App Now

Leave a Reply

Your email address will not be published.

Pay in your preferred currency
Indian rupee
Euro