[英]Wordpress: filter posts by category in pages NOT WORKING?
For over 3 days I am trying to filter posts in certain blog pages by certain categories on my wordpress theme: "Grafika".For example: I create a blog page called "Friends", after that I create a category named "friends" and after that I create 5 posts and I assign the "friends" category to those 5 posts.How can I do that on the "Friends" page to display only the posts from the category "friends".Currently the page shows all my blog posts from all pages. 在3天多的时间里,我试图按我的wordpress主题“ Grafika”中的某些类别过滤某些博客页面中的帖子。例如:创建一个名为“ Friends”的博客页面,然后创建一个名为“ friends”的类别,之后,我创建5个帖子,并为这5个帖子分配“朋友”类别。如何在“朋友”页面上仅显示“朋友”类别中的帖子。当前页面显示我的所有博客帖子从所有页面。
I have tried many many plugins, query_posts, query_args, shortcut codes in page, modifying template. 我尝试了很多插件,query_posts,query_args,页面中的快捷方式代码,修改模板。 Actually I have only 1 plugin wich almost fixed my problem.The plugin it's called "wp posts filter".But it doesn't fully work.The problem with this plugin is that I am applying a filter for Home Page and that filter goes to all my pages no matter that for other pages I applyied different filters.This is the plugin link: here 实际上,我只有1个插件可以解决我的问题。该插件称为“ wp posts filter”。但是它不能完全正常工作。该插件的问题是我正在为Home Page应用一个过滤器,并且该过滤器转到我的所有页面,无论其他页面,我都应用了不同的过滤器。这是插件链接: 此处
Can somebody give me a REALLY WORKING solution to filter posts display by category on pages ? 有人可以给我一个真正有效的解决方案,以按页面上的类别过滤显示的帖子吗? Thank you very much for reading ! 非常感谢您的阅读!
In your functions.php
file of your theme, we can use the pre_get_posts
function to alter the query before the page load. 在您主题的functions.php
文件中,我们可以使用pre_get_posts
函数在页面加载之前更改查询。
function my_friends_category( $query ) {
if ( $query->is_page('friends')):
$query->set( 'cat', 'friends' );
endif;
}
add_action( 'pre_get_posts', 'my_friends_category' );
This is assume the name of your page is friends
otherwise replace it with the id
of the page. 假设您的页面名称是friends
否则将其替换为页面id
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.