如何在WordPress主题中仅显示具有某些类别的帖子?
我想知道是否有一种方法可以更改此代码以仅显示在 WordPress 中创建的特定类别的帖子。现在它显示每一个最近的帖子。假设我要在 WordPress 中创建“新闻”类别,并且我希望这段代码仅显示新闻帖子。
感谢您的帮助
<?php
if( have_posts() ){
while( have_posts() ){
the_post();
get_template_part( 'template-parts/content', 'koncert');
}
}
?>
I was wondering if there is a way to change this code to only display posts from certain category created in wordpress. Now it displays every recent post. Let's say I would create "News" category in wordpress and I want this piece of code to display only News posts.
Thanks for help
<?php
if( have_posts() ){
while( have_posts() ){
the_post();
get_template_part( 'template-parts/content', 'koncert');
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以覆盖 WordPress 使用的常用查询并创建自定义查询;
https://developer.wordpress.org/reference/classes/wp_query/
通常不过,如果仅显示类别,您只需打开类别标签,只要您的模板具有正确的存档页面,它将显示该类别的帖子。
如果没有使用类似于下面的东西。您可以进一步细化搜索参数,例如上面链接中定义的帖子数量和帖子类型。
You can override the usual query that wordpress uses and create a custom one;
https://developer.wordpress.org/reference/classes/wp_query/
usually though for just displaying a category you can just open the category slug and provided your template has the correct archive page it will display the posts for that category.
If not use something similar to below. You can further refine your search parameters like number of posts and post types as defined in the link above.