繁体   English   中英

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

[英]WordPress - producing a list of posts filtered by tag and then category

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

但是,如果用户选择一个标签,将它们带到该标签存档页面,我希望它们仍然可以看到六个列表模板,但每个类别中的所有帖子也会被标签过滤掉。 因此,帖子列表首先按标签过滤,然后按类别过滤。

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

是的,我终于找到了一个相对简单的解决方案。

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

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

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

哪作得很好。

试试这段代码:

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;

根据Wordpress API ,您可以在对query_posts的调用中按标签进行过滤。

例子:

query_posts('tag=cooking');

query_posts('tag=bread,baking');

query_posts('tag=bread+baking+recipe');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM