Have you ever wanted to showcase your recent posts from each category in your WordPress sidebar? Recently, one of our users asked us for an easy way to display recent posts from a specific category in WordPress sidebar widgets. In this article, we will cover how to show recent posts by category in your WordPress sidebar.
您是否曾经想过在WordPress侧栏中显示每个类别的最新帖子? 最近,我们的一位用户要求我们提供一种简便的方法,以在WordPress 侧边栏小部件中显示特定类别的最新帖子。 在本文中,我们将介绍如何在WordPress边栏中按类别显示最新帖子。

影片教学 (Video Tutorial)
If you don’t like the video or need more instructions, then continue reading.
如果您不喜欢该视频或需要更多说明,请继续阅读。
There are two ways to display recent posts by category in WordPress. The first method is fairly simple and beginner friendly because we will use a plugin to display recent posts by category in a widget (no coding necessary).
有两种方法可以按类别显示WordPress中的最新帖子。 第一种方法非常简单并且对新手友好,因为我们将使用插件在小部件中按类别显示最近的帖子(无需编码)。
The second method uses a code snippet for our advanced DIY users, so you can display recent posts from a specific category without a plugin.
第二种方法是为高级DIY用户使用代码段,因此您可以显示特定类别的最新帖子而无需插件。
The only advantage to using the code method is that you are not dependent on a plugin, and you have a few more customization options. However the plugin method is EASY and has most of the customization options to satisfy 95% of the people such as show post thumbnail images, display post excerpt and control excerpt length, show the post date and number of comments, etc.
使用code方法的唯一好处是您不依赖于插件,并且还有更多自定义选项。 但是,插件方法很容易,并且具有大多数自定义选项,可以满足95%的需求,例如显示帖子缩略图,显示帖子摘录和控制摘录长度,显示帖子日期和评论数等。
Having that said, let’s take a look how you can can show recent posts by category in your WordPress sidebar with the category post widget plugin.
话虽如此,让我们看一下如何使用category post小部件插件在WordPress边栏中按类别显示最近的帖子。
按类别显示最新帖子(插件方式) (Display Recent Posts by Category (Plugin Method))
First thing you need to do is install and activate the Category Posts Widget plugin.
您需要做的第一件事是安装并激活Category Posts Widget插件。
Upon activation, you need to visit Appearance » Widgets, there you will notice the new Category Posts widget in the list of available widgets.
激活后,您需要访问外观»窗口小部件 ,您将在可用窗口小部件列表中看到新的类别帖子窗口小部件。
Simply drag and drop Category Posts widget to a sidebar where you want to display recent posts by category.
只需将“类别帖子”小部件拖放到侧栏,即可在其中按类别显示最近的帖子。

The widget options are quite self explanatory. First you need to provide a title for the category posts section and choose a category. After that you can choose other display options like number of posts, excerpts, featured image, etc.
小部件选项很容易解释。 首先,您需要为类别帖子部分提供标题,然后选择一个类别。 之后,您可以选择其他显示选项,例如帖子数,摘录,特色图片等。
Once you are done, click the save button to store your widget settings. You can now visit your site to see recent posts by category in action.
完成后,单击“保存”按钮以存储小部件设置。 现在,您可以访问您的网站,以按类别查看最近的帖子。
按类别显示最近发布的帖子,不带插件(代码段) (Display Recent Posts by Category without a Plugin (Code Snippet))
In this method, we will use a code snippet to display recent posts from a category.
在这种方法中,我们将使用代码片段显示类别中的最新帖子。
First you need to add this code in your theme’s functions.php file or a site-specific plugin.
首先,您需要将此代码添加到主题的functions.php文件或特定于站点的插件中 。
function wpb_postsbycategory() {
// the query
$the_query = new WP_Query( array( 'category_name' => 'announcements', 'posts_per_page' => 10 ) );
// The Loop
if ( $the_query->have_posts() ) {
$string .= '<ul class="postsbycategory widget_recent_entries">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
if ( has_post_thumbnail() ) {
$string .= '<li>';
$string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</a></li>';
} else {
// if no featured image is found
$string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
}
}
} else {
// no posts found
}
$string .= '</ul>';
return $string;
/* Restore original Post Data */
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('categoryposts', 'wpb_postsbycategory');
// Enable shortcodes in text widgets
add_filter('widget_text', 'do_shortcode');
Make sure that you replace 'announcements'
with your own category slug.
确保用您自己的类别条目替换'announcements'
。
This code simply queries WordPress to retrieve 10 posts from a specified category. It then displays the posts in a bulleted list. If a post has a featured image (post thumbnail), then it will show the featured image as well.
这段代码只是查询WordPress来检索指定类别的10个帖子。 然后,它在项目符号列表中显示帖子。 如果帖子具有精选图片(帖子缩略图),则该帖子也会显示精选图片。
In the end, we created a shortcode 'categoryposts'
and enabled shortcode in text widgets.
最后,我们创建了一个短代码'categoryposts'
并在文本小部件中启用了短代码。
There are three ways of displaying the recent posts by category using this code snippet.
使用此代码段,可以通过三种方式按类别显示最近的帖子。
First, you can simply paste the following code anywhere in your desired template file location (such as footer.php, single.php, etc).
首先,您可以简单地将以下代码粘贴到所需模板文件位置的任何位置(例如footer.php,single.php等)。
<?php wpb_postsbycategory() ?>
Second and third method relies on using the shortcode in the widget area or inside your posts / pages.
第二种和第三种方法依赖于在小部件区域或帖子/页面内部使用简码。
Simply visit Appearance » Widgets and add a text widget to your sidebar. Next add [categoryposts]
shortcode in the text widget and save it. You can now preview your website to see recent posts by category in the sidebar.
只需访问外观»小部件,然后在边栏中添加一个文本小部件。 接下来,在文本小部件中添加[categoryposts]
简码并保存。 现在,您可以预览您的网站,以在侧边栏中按类别查看最近的帖子。
If you want to show recent posts by categories on specific post or pages, then simply paste the shortcode in the post content area.
如果要按类别显示特定帖子或页面上的最新帖子,则只需将简码粘贴到帖子内容区域。
By default, your list may not look very good. You will need to use CSS to style the category posts list. You can use the code below as an starting point in your theme or child theme’s stylesheet.
默认情况下,您的列表可能看起来不太好。 您将需要使用CSS设置类别帖子列表的样式。 您可以将以下代码用作主题或子主题的样式表的起点。
ul.postsbycategory {
list-style-type: none;
}
.postsbycategory img {
float:left;
padding:3px;
margin:3px;
border: 3px solid #EEE;
}

That’s all, we hope this article helped you display recent posts by category in WordPress sidebar. You may also want to check out these most wanted category hacks and plugins for WordPress.
仅此而已,我们希望本文能帮助您在WordPress侧边栏中按类别显示最新帖子。 您可能还需要查看WordPress这些最想要的类别黑客和插件 。
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Google+.
如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在Twitter和Google+上找到我们。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-show-recent-posts-by-category-in-wordpress/