使用URL参数列出类别和自定义分类中的帖子

时间:2013-07-01 作者:its_me

例如http://example.com/?cat=6&tag=books 列出属于类别ID 6并标记为“books”的帖子(即同时满足这两个条件的帖子)。

同样地,http://example.com/feed/?cat=6&tag=books 对提要执行相同的操作。

现在,假设我的博客有一个自定义的分类法,叫做“版本”,下面有“美国”、“英国”、“中国”等术语。URLhttp://example.com/?edition=usa,china 列出属于“美国”和“中国”两个版本的帖子。

http://example.com/category/cars/?edition=usa 列出“汽车”类别中的帖子that also belong to 自定义分类术语“美国”。

问题是,我在函数中使用了下面的代码。php:

add_filter(\'pre_get_posts\',\'better_editions_archive\');
function better_editions_archive( $better_editions_query ) {

    /* Looks like this line needs to be changed, not sure how */
    if ( $better_editions_query->is_tax( \'edition\' ) && $better_editions_query->is_main_query() ) {

        $better_editions_terms = get_terms( \'edition\', array( \'fields\' => \'ids\' ) );
        $better_editions_query->set( \'post_type\', array( \'post\' ) );
        $better_editions_query->set( \'tax_query\',
            array(
                \'relation\' => \'OR\',
                array(
                    \'taxonomy\' => \'edition\',
                    \'field\' => \'id\',
                    \'terms\' => $better_editions_terms,
                    \'operator\' => \'NOT IN\'
                )
            )
        );

    }

    return $better_editions_query;
}
该代码确保,如果某篇文章未分配给任何版本(即,如果某篇文章未分配给属于自定义分类法“版本”的任何术语),则该文章将显示/列出在自定义分类法“版本”的所有术语存档/提要下。

现在http://example.com/category/cars/?edition=usa 仅列出属于“汽车”类别的帖子and specifically marked \'美国”(属于自定义分类法“版本”的术语)。它不会显示那些在自定义分类法“edition”中未分配给任何术语的帖子。如何修复此问题?

(PS: Setting default terms for posts 不是选项,因为我们以后可能会添加更多版本。)

已解决,但

我已经想出了一个解决方案,你可以把它看作是这个问题的答案。悬赏仍然有效,所以请放心。:)

2 个回复
最合适的回答,由SO网友:its_me 整理而成

一般情况下,使用URL参数,可以列出属于特定类别和自定义分类法的帖子,如下所示:

http://example.com/category/cars/?edition=usa
其中,category 是您用于站点上类别的类别库(WordPress Dashboard>Settings>Permalinks>Category base);edition 是自定义分类法的基/段;和usa 是自定义分类法下的术语。

如果要包含多个类别/自定义分类法,以下选项可能会有所帮助:

http://example.com/category/cars/?edition=usa,india
http://example.com/?category_name=cars,books&edition=usa,india
和提要:

http://example.com/category/cars/?edition=usa,india&feed=rss2
http://example.com/?category_name=cars,books&edition=usa,india&feed=rss2

Further Reading:

  • http://codex.wordpress.org/WordPress_Feeds

  • http://codex.wordpress.org/Class_Reference/WP_Query

    正如我在问题中所解释的,我的情况很复杂,所以我制定了一个简单的解决方法。这是。。。

    问题中的代码块确保,如果某篇文章未分配给任何版本(即,如果某篇文章未分配给属于自定义分类法“版本”的任何术语),则该文章将显示/列出在自定义分类法“版本”的所有术语存档/提要下。

    BUT NOW, 我删除了那个代码。然后在自定义分类法“edition”下创建了一个新术语,称为“intl”(国际)。任何想要在所有版本下显示的帖子都将被分配到“intl”。但我如何确保分配给“intl”的所有帖子都显示在我自定义分类法的所有术语归档/提要中?

    为此,我现在使用以下代码(在functions.php中):

    add_filter(\'pre_get_posts\',\'better_editions_archive\');
    function better_editions_archive( $query ) {
        if ( $query->is_tax( \'edition\' ) && $query->is_main_query() ) {
            $query->set( \'post_type\', array( \'post\' ) );
            $query->set( \'tax_query\',
                array(
                    \'relation\' => \'OR\',
                    array(
                        \'taxonomy\' => \'edition\',
                        \'field\' => \'slug\',
                        \'terms\' => \'intl\',
                        \'operator\' => \'IN\'
                    )
                )
            );
        }
        return $query;
    }
    
    比如现在,http://example.com/edition/usa/ 列出属于“usa”或“intl”(这是我的自定义分类“edition”下的术语)的帖子。它的饲料,http://example.com/edition/usa/feed/ 同样的道理。

    回到问题的主要问题。如何使用URL参数列出属于特定类别和版本的帖子?

    例如,我如何列出属于“汽车”类别和“美国”版本的帖子?

    这是URL的外观:http://example.com/category/cars/?edition=usa,intl (因为我们还需要在所有版本下显示的帖子,即分配给术语“intl”的帖子)。对于提要:http://example.com/category/cars/feed/?edition=india,intl

    就是这样!

    (特别感谢@kaiser 感谢他的帮助。)

    如果您严格希望修改模板中的主查询/循环,请注意,例如分类法版本。php(在我的例子中),下面是一个如何实现的示例:

    <?php
    $edition_term = get_term( get_queried_object(), \'edition\' )->slug;
    $better_editions = new WP_Query(
        array(
            \'post_type\' => \'post\',
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'edition\',
                    \'field\' => \'slug\',
                    \'terms\' => array( $edition_term, \'intl\' )
                )
            )
        )
    );
    ?>
    
        <?php /* Blah, Blah, Blah! */ ?>
    
    <?php if ( $better_editions->have_posts() ) : ?>
    
        <?php /* Start the Loop */ ?>
        <?php while ( $better_editions->have_posts() ) : $better_editions->the_post(); ?>
            <?php get_template_part( \'content\', get_post_format() ); ?>
        <?php endwhile; wp_reset_postdata(); ?>
    
    <?php else : ?>
    
        <?php /* Blah, Blah, Blah! */ ?>
    
    <?php endif; ?>
    
    尽管如此,除非你真的,真的必须pre_get_posts.

SO网友:Greeso

您可能需要研究使用wp\\u查询。在其中,您可以设置自己的查询以显示您喜欢的任何类别,然后在完成后,将其重置为默认行为。

结束

相关推荐

Link categories to last post

我正在使用wp\\u list\\u categories()显示我的所有类别。但我希望子类别链接到该类别的最后一个帖子。例如: <ul> <li><a href=\"link-to-category-1\">CATEGORY 1</a> <ul> <li><a href=\"link-to-last-post-of-category-1-1\">CATE