概述
Do you want to display recent posts from a specific category in WordPress? The default recent posts widget shows posts from all categories, and there is no option to filter them by category. In this article, we will show you how to easily display recent posts from a specific category in WordPress.
您是否要显示WordPress中特定类别的最新帖子? 默认的“最近帖子”小部件显示所有类别的帖子,并且没有按类别过滤帖子的选项。 在本文中,我们将向您展示如何轻松显示WordPress中特定类别的最新帖子。
在WordPress中按类别过滤帖子 (Filtering Posts by Category in WordPress)
If you just want to create a page to display recent posts from a particular category, then your WordPress site already has separate pages for each category.
如果您只想创建一个页面来显示特定类别的最新帖子,那么您的WordPress网站已经为每个类别提供了单独的页面。
You can add links to all your category pages by visiting Appearance » Widgets page and adding the ‘Categories’ widget to your sidebar. You can also add categories in your navigation menus.
您可以通过访问外观»小部件页面并将“类别”小部件添加到侧边栏中来添加指向所有类别页面的链接。 您也可以在导航菜单中添加类别 。
On the other hand, if you want to show recent posts from a specific category in your sidebar, then there is no default widget for that. The default recent posts widget does not allow you to filter posts by category or tags.
另一方面,如果要在侧边栏中显示特定类别的最新帖子,则没有默认的小部件。 默认的“最近帖子”小部件不允许您按类别或标签过滤帖子。
Thankfully there is another way. Let’s take a look at how to easily display recent posts from specific category in WordPress.
幸运的是,还有另一种方法。 让我们看一下如何轻松显示WordPress中特定类别的最新帖子。
影片教学 (Video Tutorial)
演示地址
If you don’t like the video or need more instructions, then continue reading.
如果您不喜欢该视频或需要更多说明,请继续阅读。
方法1.使用插件显示类别中的最新帖子 (Method 1. Show Recent Posts from a Category Using Plugin)
This method is easier, and it is recommended for most users.
此方法更简单,建议大多数用户使用。
First thing you need to do is install and activate the Recent Posts Widget Extended plugin. For more details, see our step by step guide on how to install a WordPress plugin.
您需要做的第一件事是安装并激活“ 最近发布的窗口小部件扩展”插件。 有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。
Upon activation, you need to visit the Appearance » Widgets page and add ‘Recent Posts Extended’ widget to your sidebar.
激活后,您需要访问“ 外观»小部件”页面,并将“扩展的最新帖子”小部件添加到侧边栏中。
The widget menu will expand to show its settings. You need to select the category or categories that you want to display under the ‘Limit to Category’ option.
小部件菜单将展开以显示其设置。 您需要选择要在“限制到类别”选项下显示的一个或多个类别。
The widget comes with a lot of options that you can customize. You can show post thumbnail, date, relative date, post summary / excerpts, and more.
该小部件带有许多您可以自定义的选项。 您可以显示帖子缩略图 ,日期, 相对日期 , 帖子摘要/摘录等。
Don’t forget to click on the save button to store your widget settings.
不要忘记单击“保存”按钮来存储您的小部件设置。
You can now visit your website to see the recent posts displayed by category.
现在,您可以访问您的网站,以查看按类别显示的最新帖子。
Display Recent Posts by Category Using Shortcode
使用简码显示按类别显示的最新帖子
The Recent Posts Extended Widget also allows you to use shortcode to display recent posts anywhere on your site including posts and pages.
“最近发布的帖子扩展小部件”还允许您使用简码在网站上的任何位置显示最近发布的帖子,包括帖子和页面。
You will need to edit the post or page where you want to display the recent posts from a specific category. In the post editor, you will need to add the following shortcode:
您将需要编辑要显示特定类别的最新帖子的帖子或页面。 在帖子编辑器中,您将需要添加以下短代码:
[rpwe limit="5" excerpt="true" cat="72" ]
[rpwe limit="5" excerpt="true" cat="72" ]
This shortcode displays 5 recent posts from a specific category with the post excerpt. You will need to replace the cat value with the ID of the category that you want to display. See our article on how to find category ID in WordPress.
该短代码显示5个特定类别的最新帖子,并附有帖子摘录。 您将需要用要显示的类别的ID替换cat值。 请参阅有关如何在WordPress中查找类别ID的文章。
After adding the shortcode, you can save your post or page to view your changes.
添加简码后,您可以保存帖子或页面以查看更改。
方法2。使用代码片段显示特定类别的最新帖子 (Method 2. Display Recent Posts From Specific Category using Code Snippet)
This method requires you to add code to your WordPress theme files. If you haven’t done this before, then take a look at our guide on how to copy and paste code in WordPress.
此方法要求您将代码添加到WordPress主题文件中。 如果您以前没有做过,请查看我们的指南, 了解如何在WordPress中复制和粘贴代码 。
You will need to add the following code in your WordPress theme files where you want to display recent posts from a specific category.
您需要在WordPress主题文件中添加以下代码,以在其中显示特定类别的最新帖子。
<?php $catquery = new WP_Query( 'cat=72&posts_per_page=5' ); ?>
<ul>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile;
wp_reset_postdata();
?>
The first line of this code creates a new WordPress query with a specific category ID. You need to replace it with your own category ID. It only shows post title in a list.
此代码的第一行创建一个具有特定类别ID的新WordPress查询。 您需要用自己的类别ID替换它。 它仅在列表中显示帖子标题。
You can change it to display full content by adding the following code:
您可以通过添加以下代码将其更改为显示完整内容:
<?php $catquery = new WP_Query( 'cat=72&posts_per_page=5' ); ?>
<ul>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<li><h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
<?php endwhile; ?>
</ul>
<?php wp_reset_postdata(); ?>
You can also replace the the_content
with the_excerpt
to display post excerpts instead of full article.
您也可以替换the_content
与the_excerpt
显示帖子摘录,而不是完整的文章。
We hope this article showed you how to easily display recent posts from a specific category in WordPress. You may also want to see our list of 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 Facebook.
如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在Twitter和Facebook上找到我们。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-display-recent-posts-from-a-specific-category-in-wordpress/
最后
以上就是勤恳音响为你收集整理的如何在WordPress中显示特定类别的最新帖子的全部内容,希望文章能够帮你解决如何在WordPress中显示特定类别的最新帖子所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复