You’re probably familiar with the Category
widget in WordPress. Recently, one of our readers asked us if it was
possible to display recent posts in a drop down as well. In this
article, we will show you how to show recent posts as a drop down in
WordPress.
This widget simply displays a list of recent posts, and you can choose the number of posts you want to show. But if you want to show more than 5-10 posts, then the list will take a lot of space in your sidebar.
Some WordPress users may need a compact way to display recent posts. In that case, using drop downs or collapsible lists can help you save space.
Let’s take a look at couple of different ways to show recent posts as a drop down menu in WordPress.
Now you can use the shortcode
First thing you need to do is install and activate the Collapse-O-Matic plugin. It works out of the box, and there are no settings for you to configure.
The plugin simply allows you to show anything in a collapsible menu using a shortcode.
Before we use this 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.
This code simply allows you to display a list of recent posts using the shortcode
Now we will add our shortcode in the Collapse-O-Matic shortcode to create a collapsible list of recent posts.
Simply add the shortcode like this:
You can add this shortcode in a text widget, posts, or pages on your WordPress site. This is how it looked on our test site.
Why and Who Needs Recent Posts in Drop Down?
WordPress comes with a built-in recent posts widget that you can add to any sidebar or widget ready area.This widget simply displays a list of recent posts, and you can choose the number of posts you want to show. But if you want to show more than 5-10 posts, then the list will take a lot of space in your sidebar.
Some WordPress users may need a compact way to display recent posts. In that case, using drop downs or collapsible lists can help you save space.
Let’s take a look at couple of different ways to show recent posts as a drop down menu in WordPress.
Showing WordPress Recent Posts in a Plain Drop Down Menu (Manual Code)
This method uses the built-inwp_get_recent_posts
function. All you need to do is copy and paste the following code in your theme’s functions.php file or a site-specific plugin. 01 | function wpb_recentposts_dropdown() { |
02 | $string .= '<select id= "rpdropdown" > |
03 | <option value= "" selected>Select a Post<option>'; |
04 |
05 | $args = array ( 'numberposts' => '5' , 'post_status' => 'publish' ); |
06 |
07 | $recent_posts = wp_get_recent_posts( $args ); |
08 | foreach ( $recent_posts as $recent ){ |
09 | $string .= '<option value="' . get_permalink( $recent ["ID "]) . '" >' . $recent [ "post_title" ]. '</option> ' ; |
10 | } |
11 |
12 | $string .= '</select> |
13 | <script type= "text/javascript" > var urlmenu = document.getElementById( "rpdropdown" ); urlmenu.onchange = function () { |
14 | window.open( this.options[ this.selectedIndex ].value, "_self" ); |
15 | }; |
16 | </script>'; |
17 |
18 | return $string ; |
19 | } |
20 | add_shortcode( 'rp_dropdown' , 'wpb_recentposts_dropdown' ); |
21 | add_filter( 'widget_text' , 'do_shortcode' ); |
[rp_dropdown]
in your WordPress post, pages, and text widgets. It will look like this: Adding Collapsible Recent Posts Using Plugin
The above method simply lists your recent posts in a drop down form. Another way to save space is by adding a collapsible list of recent posts which expands when users click on it.First thing you need to do is install and activate the Collapse-O-Matic plugin. It works out of the box, and there are no settings for you to configure.
The plugin simply allows you to show anything in a collapsible menu using a shortcode.
Before we use this 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.
01 | function wpb_recentposts() { |
02 |
03 | $string .= '<ul>' ; |
04 | $args = array ( 'numberposts' => '5' , 'post_status' => 'publish' ); |
05 | $recent_posts = wp_get_recent_posts( $args ); |
06 | foreach ( $recent_posts as $recent ){ |
07 | $string .= '<li><a href="' . get_permalink( $recent ["ID "]) . '" >' . $recent [ "post_title" ]. '</a></li> ' ; |
08 | } |
09 | $string .= '</ul>' ; |
10 | return $string ; |
11 | } |
12 | add_shortcode( 'recentposts' , 'wpb_recentposts' ); |
13 | add_filter( 'widget_text' , 'do_shortcode' ); |
[recentposts]
.Now we will add our shortcode in the Collapse-O-Matic shortcode to create a collapsible list of recent posts.
Simply add the shortcode like this:
[expand title="Recent Posts"][recentposts][/expand]
You can add this shortcode in a text widget, posts, or pages on your WordPress site. This is how it looked on our test site.
0 comments:
Post a Comment