WordPress - 生成按标签和类别过滤的帖子列表

2022-01-18 00:00:00 tags php mysql wordpress

我正在尝试创建一个页面上有六个列表的 WordPress 网站,每个列表显示来自不同类别的帖子.很简单.

I'm trying to make a WordPress site that has six lists on a page, each list showing posts from a different category. 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.

据我所知,使用 query_posts 或其他任何东西都无法做到这一点,它需要更高级的数据库使用,但我不知道如何做到这一点!我认为这里有一个类似的问题,但是因为我对 PHP 知之甚少,也没有 MySQL,所以我无法理解答案!

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! 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!

推荐答案

对了,我终于找到了一个比较简单的解决方案.

Right, I have finally found a relatively simple solution to this.

WordPress 中存在一个错误,阻止了对类别和标签的查询,因此 query_posts('cat=2&tag=bread'); 不起作用,但解决此问题的方法是query_posts('cat=2&tag=bread+tag=bread'); 神奇地起作用.

There's a bug in WordPress preventing a query of both category and tags working, so query_posts('cat=2&tag=bread'); wouldn't work, but a way around this is query_posts('cat=2&tag=bread+tag=bread'); which magically works.

在 tag.php 模板中,我希望它从该存档中获取标签,所以我必须这样做:

In a tag.php template, I wanted it to pick up the tag from that archive, so I had to do this:

<?php query_posts('cat=12&tag='.$_GET['tag'].'+'.$_GET['tag']); ?>

效果很好.

相关文章