如何显示特定术语的帖子列表,而不是与子术语关联的帖子?

时间:2018-11-01 作者:Robert Andrews

这是WordPress的正常行为吗?

我有:

自定义帖子类型:“article”(文章)自定义分类:“company”(分层)在后端,当我转到“Articles”(文章)的帖子列表,并通过单击“company”(公司)术语来约束结果时,我希望只看到标记有特定公司的帖子。

相反,我也看到了那些贴着子公司标签的plus帖子。

示例:如果单击我的“AT&;T”“company”术语(9),我不仅会看到标记有“AT&;T”的“文章”,还会看到标记有多个子级的所有下游子术语的文章(42)。。。

电话(&L);美国有线电视新闻网(CNN)的特纳广播公司(Turner Broadcasting)的董事会觉得行为不正确。

下面是我如何注册“公司”分类法。。。

/**
 * ==============================================================================
 *                      REGISTER TAXONOMY
 * ==============================================================================
 */

if ( ! function_exists( \'register_taxonomy_company\' ) ) {

function register_taxonomy_company() {

  $labels = array(
        \'name\'                       => _x( \'Organisations\', \'Taxonomy General Name\', \'text_domain\' ),
        \'singular_name\'              => _x( \'Organisation\', \'Taxonomy Singular Name\', \'text_domain\' ),
        \'menu_name\'                  => __( \'Organisations\', \'text_domain\' ),
        \'all_items\'                  => __( \'All Organisations\', \'text_domain\' ),
        \'parent_item\'                => __( \'Parent Organisation\', \'text_domain\' ),
        \'parent_item_colon\'          => __( \'Parent Organisation:\', \'text_domain\' ),
        \'new_item_name\'              => __( \'New Organisation Name\', \'text_domain\' ),
        \'add_new_item\'               => __( \'Add New Organisation\', \'text_domain\' ),
        \'edit_item\'                  => __( \'Edit Organisation\', \'text_domain\' ),
        \'update_item\'                => __( \'Update Organisation\', \'text_domain\' ),
        \'view_item\'                  => __( \'View Organisation\', \'text_domain\' ),
        \'separate_items_with_commas\' => __( \'Separate Organisations with commas\', \'text_domain\' ),
        \'add_or_remove_items\'        => __( \'Add or remove Organisation\', \'text_domain\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used\', \'text_domain\' ),
        \'popular_items\'              => __( \'Popular Organisation\', \'text_domain\' ),
        \'search_items\'               => __( \'Search Organisation\', \'text_domain\' ),
        \'not_found\'                  => __( \'Not Found\', \'text_domain\' ),
        \'no_terms\'                   => __( \'No Organisation\', \'text_domain\' ),
        \'items_list\'                 => __( \'Organisations list\', \'text_domain\' ),
        \'items_list_navigation\'      => __( \'Organisations list navigation\', \'text_domain\' ),
        );
    $rewrite = array(
        \'slug\'                       => \'organisation\',
        \'with_front\'                 => true,
        \'hierarchical\'               => true,
    );
        $args = array(
            \'labels\'                     => $labels, // as above
            \'public\'                     => true,
            \'show_ui\'                    => true,
            \'show_in_nav_menus\'          => true,
            \'hierarchical\'               => true,
            \'show_admin_column\'          => true,
        \'single_value\'               => false, // Use single-select radio button, only one Organisation per object
            \'show_tagcloud\'              => true,
            \'rewrite\'                    => $rewrite,  // as above
        );

    // Put it all together!
    register_taxonomy(
        /* taxonomy name */         \'company\',
        /* attach to object */  array( \'article\',\'report\',\'session\',\'quote\',\'post\' ),
        /* arguments */                 $args
    );

}
add_action( \'init\', \'register_taxonomy_company\', 0 );

}
这就是我注册“文章”帖子类型的方式。。。

/* Register custom post type */

function cpt_article() {

    $labels = array(
        \'name\'                  => \'Articles\',
        \'singular_name\'         => \'Article\',
        \'menu_name\'             => \'Articles\',
        \'name_admin_bar\'        => \'Article\',
        \'archives\'              => \'Article Archives\',
        \'parent_item_colon\'     => \'Parent Article:\',
        \'all_items\'             => \'All Articles\',
        \'add_new_item\'          => \'Add New Article\',
        \'add_new\'               => \'Add New\',
        \'new_item\'              => \'New Article\',
        \'edit_item\'             => \'Edit Article\',
        \'update_item\'           => \'Update Article\',
        \'view_item\'             => \'View Article\',
        \'search_items\'          => \'Search Article\',
        \'not_found\'             => \'Not found\',
        \'not_found_in_trash\'    => \'Not found in Trash\',
        \'featured_image\'        => \'Featured Image\',
        \'set_featured_image\'    => \'Set featured image\',
        \'remove_featured_image\' => \'Remove featured image\',
        \'use_featured_image\'    => \'Use as featured image\',
        \'insert_into_item\'      => \'Insert into Article\',
        \'uploaded_to_this_item\' => \'Uploaded to this Article\',
        \'items_list\'            => \'Articles list\',
        \'items_list_navigation\' => \'Articles list navigation\',
        \'filter_items_list\'     => \'Filter Articles list\',
    );
    $args = array(
        \'label\'                 => \'Article\',
        \'description\'           => \'Articles.\',
        \'labels\'                => $labels,
        \'supports\'              => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', /*\'comments\', \'revisions\', \'post-formats\',*/ ),
        \'taxonomies\'            => array( \'source\', \'category\', \'post_tag\' ),
        \'hierarchical\'          => false,
        \'public\'                => true,
        \'show_ui\'               => true,
        \'show_in_menu\'          => true,
        \'menu_position\'         => 6,
        \'menu_icon\'             => \'dashicons-media-text\',
        \'show_in_admin_bar\'     => true,
        \'show_in_nav_menus\'     => true,
        \'can_export\'            => true,
        \'has_archive\'           => true,
        \'exclude_from_search\'   => false,
        \'publicly_queryable\'    => true,
        \'capability_type\'       => \'post\',
    );
    register_post_type( \'article\', $args );

}
add_action( \'init\', \'cpt_article\', 0 );
hierarchical 已设置为true. 我已将其更改为false, 但没有效果。

更新:

我刚刚检查了另一个单独的WordPress安装,它运行分层分类法,并确认了第二种情况——当查看某个术语的帖子时,WordPress管理帖子列表似乎认为为其子术语显示帖子是合适的行为。

我不敢相信这是对的。

有人知道抑制这种现象的方法吗?

这使得事情很难管理。

1 个回复
SO网友:Robert Andrews

正如@Milo的指针所说,这:

遗憾的是,这是WordPress的正常行为

  • 可以使用parse_tax_queryinclude_children 设置为0...
  • 答案中的代码位于https://wordpress.stackexchange.com/a/202773/39300 对我有用。。。

    function taxonomy_archive_exclude_children($query){
       $taxonomy_slugs = [\'product_category\', \'application_category\'];
       if($query->is_main_query() && is_tax($taxonomy_slugs)){
          foreach($query->tax_query->queries as &$tax_query_item){
            if(empty($taxonomy_slugs) || in_array($tax_query_item[\'taxonomy\'], $taxonomy_slugs)){
                $tax_query_item[\'include_children\'] = 0;
            }
          }
       }
     }
     add_action(\'parse_tax_query\', \'taxonomy_archive_exclude_children\');
    //  An empty $taxonomy_slugs array will exclude children for all taxonomy archives.
    
    parse_tax_query 分类时发生火灾,include_children 0是我可以拒绝孩子出现的地方。

    结束

    相关推荐

    get the custom taxonomy name?

    在我的functions.php 我有个钩子:add_action( \'woocommerce_before_single_product\', \'display_category_list\',20 ); function display_category_list() { wc_get_template( \'woocommerce/single-product/single-product-top- content.php\' );