wordpress下拉菜单
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.
您可能熟悉WordPress中的Category小部件。 最近,一位读者问我们是否也可以在下拉列表中显示最近的帖子。 在本文中,我们将向您展示如何以WordPress的下拉列表形式显示最近的帖子。
为什么以及谁需要下拉列表中的最新帖子? (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.
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.
该小部件仅显示最近发布的列表,您可以选择要显示的发布数量。 但是,如果您想显示5-10个以上的帖子,则该列表将在边栏中占用大量空间。
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.
一些WordPress用户可能需要一种紧凑的方式来显示最新帖子。 在这种情况下,使用下拉列表或可折叠列表可以帮助您节省空间。
Let’s take a look at couple of different ways to show recent posts as a drop down menu in WordPress.
让我们看一下几种不同的方式来显示最近的帖子作为WordPress中的下拉菜单。
在普通的下拉菜单中显示WordPress最近的帖子(手动代码) (Showing WordPress Recent Posts in a Plain Drop Down Menu (Manual Code))
This method uses the built-in wp_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.
此方法使用内置的wp_get_recent_posts
函数。 您需要做的就是将以下代码复制并粘贴到主题的functions.php文件或特定于站点的插件中 。
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');
Now you can use the shortcode [rp_dropdown]
in your WordPress post, pages, and text widgets. It will look like this:
现在,您可以在WordPress帖子,页面和文本小部件中使用简码 [rp_dropdown]
。 它看起来像这样:
使用插件添加可折叠的最新帖子 (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.
您需要做的第一件事是安装并激活Collapse-O-Matic插件。 它开箱即用,没有可供您配置的设置。
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.
在使用此插件之前,我们需要一种方法可以轻松地在需要的任何地方显示最新帖子。 只需将此代码添加到主题的functions.php文件或特定于站点的插件中即可 。
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 allows you to display a list of recent posts using the shortcode [recentposts]
.
此代码仅允许您使用简码[recentposts]
显示最近的帖子列表。
Now we will add our shortcode in the Collapse-O-Matic shortcode to create a collapsible list of recent posts.
现在,我们将我们的简码添加到Collapse-O-Matic简码中,以创建可折叠的近期帖子列表。
Simply add the shortcode like this:
只需添加如下的简码:
[expand title="Recent Posts"][recentposts][/expand]
[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.
您可以将此短代码添加到WordPress网站上的文本小部件,帖子或页面中。 这就是我们测试站点上的样子。
That’s all, we hope this article helped you show recent posts as drop down in WordPress. You may also want to see these 6 tips to create a killer editorial calendar in WordPress.
仅此而已,我们希望本文能帮助您显示WordPress中的最新帖子。 您可能还想看一下在WordPress中创建杀手级编辑日历的这6个技巧 。
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在Twitter和Facebook上找到我们。
翻译自: https://www.wpbeginner.com/wp-themes/how-to-show-recent-posts-as-a-drop-down-in-wordpress/
wordpress下拉菜单