[英]WordPress - producing a list of posts filtered by tag and then category
I'm trying to make a WordPress site that has six lists on a page, each list showing posts from a different category. 我正在尝试创建一个WordPress网站,该网站在一个页面上有六个列表,每个列表显示来自不同类别的帖子。 Simple. 简单。
But then, if a user selects a tag, taking them to that tag archive page, I want them to still see the six-list template, but all the posts within each category are also filtered by the tag. 但是,如果用户选择一个标签,将它们带到该标签存档页面,我希望它们仍然可以看到六个列表模板,但每个类别中的所有帖子也会被标签过滤掉。 So lists of posts are filtered first by tag, and then by category. 因此,帖子列表首先按标签过滤,然后按类别过滤。
As far as I can tell, there is no way of doing this using query_posts or anything, it needs more advanced use of the database, but I have no idea how to do this! 据我所知,没有办法使用query_posts或其他任何东西,它需要更高级的数据库使用,但我不知道如何做到这一点! I think that there's a similar question on here, but because I know very little PHP and no MySQL, I can't make sense of the answers! 我认为这里有一个类似的问题,但因为我知道PHP很少而且没有MySQL,所以我无法理解答案!
Right, I have finally found a relatively simple solution to this. 是的,我终于找到了一个相对简单的解决方案。
There's a bug in WordPress preventing a query of both category and tags working, so query_posts('cat=2&tag=bread');
WordPress中存在一个错误,阻止查询类别和标签,因此query_posts('cat=2&tag=bread');
wouldn't work, but a way around this is query_posts('cat=2&tag=bread+tag=bread');
不行,但解决这个问题的方法是query_posts('cat=2&tag=bread+tag=bread');
which magically works. 这神奇地起作用。
In a tag.php template, I wanted it to pick up the tag from that archive, so I had to do this: 在tag.php模板中,我希望它从该存档中获取标记,因此我必须这样做:
<?php query_posts('cat=12&tag='.$_GET['tag'].'+'.$_GET['tag']); ?>
which works perfectly. 哪作得很好。
Try this code: 试试这段代码:
query_posts('tag=selected_tag');
while (have_posts()) : the_post();
foreach((get_the_category()) as $category)
{
if ($category->cat_name == 'selected_category')
{
// output any needed post info, for example:
echo the_title();
}
}
endwhile;
According to the Wordpress API , you can filter by tags within a call to query_posts
. 根据Wordpress API ,您可以在对query_posts
的调用中按标签进行过滤。
Examples: 例子:
query_posts('tag=cooking');
query_posts('tag=bread,baking');
query_posts('tag=bread+baking+recipe');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.