使用‘PRE_GET_POST’在WordPress中创建交叉推广类别

时间:2013-10-31 作者:CentDEV

我想做什么

为一篇文章分配多个类别,使其出现在每个类别模板中,而URL中没有一个主类别。

为一篇文章选择多个类别时,WP的默认行为是将文章分配给ID最低的类别。

例如:如果;苹果公司;类别的ID为6和;桃子“;类别的ID为4。如果帖子被添加到Apple类别中,那么永久链接将是/苹果/帖子标题(相当永久链接)。如果将帖子添加到Apple和Peach类别,则永久链接将是/Peach/帖子标题。

约束必须出现在permalink中,如果添加了其他类别,Permalinks将无法更改。首先,我创建了一个名为;跨类别;。这将允许我创建与主类别同名的第二组类别。这样我就可以选择苹果作为分类,桃子作为;跨类别"E;

function crosspromote_taxonomies() {
    $crosscatlabels = array(
        \'name\'              => _x( \'Cross Promote Categories\', \'taxonomy general name\' ),
        \'singular_name\'     => _x( \'Cross Promote Category\', \'taxonomy singular name\' ),
        \'search_items\'      => __( \'Search Cross Promote Categories\' ),
        \'popular_items\'     => __( \'Popular Cross Promote Categories\' ),
        \'all_items\'         => __( \'All Cross Promote Categories\' ),
        \'parent_item\'       => __( \'Parent Cross Promote Category\' ),
        \'parent_item_colon\' => __( \'Parent Cross Promote Category:\' ),
        \'edit_item\'         => __( \'Edit Cross Promote Category\' ), 
        \'update_item\'       => __( \'Update Cross Promote Category\' ),
        \'add_new_item\'      => __( \'Add New Cross Promote Category\' ),
        \'new_item_name\'     => __( \'New Cross Promote Category\' ),
    );
    register_taxonomy( 
        \'crosscategory\', 
        array(
            \'post\'
        ), 
        array( 
            \'hierarchical\'      => true, 
            \'labels\'            => $crosscatlabels,
            \'show_ui\'           => true,
            \'query_var\'         => true,
            \'rewrite\'           => false,
        )
    );
}
现在我需要创建一个只在类别上运行的过滤器。php模板。如果我导航到/苹果/我需要查看苹果类别中的所有帖子,以及其他类别中的任何帖子,这些帖子在苹果也有交叉推广。

function cross_category_filter($query){
    // only run this filter on the actual category page
    if ( is_admin() || $query->is_home() || !$query->is_main_query() || $query->is_singular || !$query->is_category )
        return;

    $category_name = $query->query_vars[\'category_name\'];

    // Required to include posts from category-x used for cross-promoting posts in other categories without changing the URL
    $query->set( 
        \'tax_query\', 
        array(
            \'relation\' => \'OR\',
            array(
                \'taxonomy\'  => \'crosscategory\',
                \'field\'     => \'slug\',
                \'terms\'     => array($category_name),
                \'operator\'  => \'IN\'
            )
        ) 
    );
}
add_action( \'pre_get_posts\', \'cross_category_filter\', 1 );

The Problem

这似乎根本不起作用。当我导航到/apple时,我要么看到标签页(而不是类别模板——不知道为什么),要么根本看不到帖子。标题。php,侧栏。php和页脚。php会加载,但帖子应该是空的,就像has\\u posts()没有帖子一样。

$查询对象的输出如下所示:

WP_Query Object
(
    [query_vars] => Array
        (
            [category_name] => apple
            [error] => 
            [m] => 0
            [p] => 0
            [post_parent] => 
            [subpost] => 
            [subpost_id] => 
            [attachment] => 
            [attachment_id] => 0
            [name] => 
            [static] => 
            [pagename] => 
            [page_id] => 0
            [second] => 
            [minute] => 
            [hour] => 
            [day] => 0
            [monthnum] => 0
            [year] => 0
            [w] => 0
            [tag] => 
            [cat] => 
            [tag_id] => 
            [author_name] => 
            [feed] => 
            [tb] => 
            [paged] => 0
            [comments_popup] => 
            [meta_key] => 
            [meta_value] => 
            [preview] => 
            [s] => 
            [sentence] => 
            [fields] => 
            [menu_order] => 
            [category__in] => Array
                (
                )

            [category__not_in] => Array
                (
                )

            [category__and] => Array
                (
                )

            [post__in] => Array
                (
                )

            [post__not_in] => 
            [tag__in] => Array
                (
                )

            [tag__not_in] => Array
                (
                )

            [tag__and] => Array
                (
                )

            [tag_slug__in] => Array
                (
                )

            [tag_slug__and] => Array
                (
                )

            [tax_query] => Array
                (
                    [relation] => OR
                    [0] => Array
                        (
                            [taxonomy] => crosscategory
                            [field] => slug
                            [terms] => Array
                                (
                                    [0] => apple
                                )

                            [operator] => IN
                        )

                )

        )

    [tax_query] => WP_Tax_Query Object
        (
            [queries] => Array
                (
                    [0] => Array
                        (
                            [taxonomy] => category
                            [terms] => Array
                                (
                                    [0] => apple
                                )

                            [include_children] => 1
                            [field] => slug
                            [operator] => IN
                        )

                )

            [relation] => AND
        )

    [meta_query] => 
    [post_count] => 0
    [current_post] => -1
    [in_the_loop] => 
    [comment_count] => 0
    [current_comment] => -1
    [found_posts] => 0
    [max_num_pages] => 0
    [max_num_comment_pages] => 0
    [is_single] => 
    [is_preview] => 
    [is_page] => 
    [is_archive] => 1
    [is_date] => 
    [is_year] => 
    [is_month] => 
    [is_day] => 
    [is_time] => 
    [is_author] => 
    [is_category] => 1
    [is_tag] => 
    [is_tax] => 
    [is_search] => 
    [is_feed] => 
    [is_comment_feed] => 
    [is_trackback] => 
    [is_home] => 
    [is_404] => 
    [is_comments_popup] => 
    [is_paged] => 
    [is_admin] => 
    [is_attachment] => 
    [is_singular] => 
    [is_robots] => 
    [is_posts_page] => 
    [is_post_type_archive] => 
    [query_vars_hash] => 47f5cb8591c9e310161e42fa4bffbf43
    [query_vars_changed] => 
    [thumbnails_cached] => 
    [query] => Array
        (
            [category_name] => apple
        )

)
我正在使用pre\\u get\\u posts来减少查询的数量,并以正确的方式进行查询。

它应该可以找到苹果类别中的所有帖子,以及苹果跨类别中的所有帖子。我假设即使跨类别Apple中没有帖子,它也应该在Apple类别中找到帖子。

这两件事都没有发生,我在这一点上被难住了。

2 个回复
SO网友:Courtney Ivey

我想我记得在某处看到过这种尝试。。。

如果我没记错的话,解决方案是对该类别使用自定义税务查询。php模板,但首先在“or”语句中查询分类法(crosspostcategory)。。。

因此,首先要检查分类法,然后在OR语句中查询(税务查询)下一个类别分类法。

我会四处看看是否能找到它。

自类别。php首先查询实际的类别分类法,我认为您必须对pre\\u get\\u posts使用的函数进行排序。英雄联盟

SO网友:gmazzap

您的tax\\u查询应该包含这两种分类法,还应该从查询中删除类别名称以避免冲突:

function cross_category_filter($query){
    // only run this filter on the actual category page
    if ( is_admin() || $query->is_home() || ! $query->is_main_query() || ! $query->is_category )
        return;

    $category_name = $query->query_vars[\'category_name\'];

    // remove the category from query
    $query->set(\'category_name\', null);

    // create a brand new tax query including both taxonomies
    $query->set( 
        \'tax_query\', 
        array(
           \'relation\' => \'OR\',
            array(
                \'taxonomy\'  => \'crosscategory\',
                \'field\'     => \'slug\',
                \'terms\'     => array($category_name),
                \'operator\'  => \'IN\'
            ),
            array(
                \'taxonomy\'  => \'category\',
                \'field\'     => \'slug\',
                \'terms\'     => array($category_name),
                \'operator\'  => \'IN\'
            )
        ) 
    );
}
add_action( \'pre_get_posts\', \'cross_category_filter\', 1 );

结束

相关推荐

Ordering Subcategories

我使用以下代码显示子类别中的数据。子类别为“英国”、“仅英格兰”、“仅北爱尔兰”、“仅苏格兰”、“仅威尔士”。按日期或asc/desc排序,英国子类别位于中间位置,威尔士或英格兰位于顶部。我需要英国子类别位于顶部。它拥有最多的帖子-那么可以在查询中使用orderby来解决这个问题吗?或者你认为我只需要为UK子类别创建自己的循环吗?$parentCatID = get_cat_ID(\'Grants and Incentives\'); $childCats = get_categories( \'