是否自定义分类和Tax_Query问题?

时间:2016-01-29 作者:raxa

我正在开发插件,我的插件名是plugindev.我有一个自定义的帖子类型,叫做team.我有一个自定义分类法Team_Category 该代码正在注册

/***************************taxonomy****************************/
add_action( \'init\', \'create_team_taxonomies\', 0 );
function create_team_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        \'name\'              => _x( \'Team_Categories\', \'taxonomy general name\' ),
        \'singular_name\'     => _x( \'Team_Category\', \'taxonomy singular name\' ),
        \'search_items\'      => __( \'Search Team_Categories\' ),
        \'all_items\'         => __( \'All Team_Categories\' ),
        \'parent_item\'       => __( \'Parent Team_Category\' ),
        \'parent_item_colon\' => __( \'Parent Team_Category:\' ),
        \'edit_item\'         => __( \'Edit Team_Category\' ),
        \'update_item\'       => __( \'Update Team_Category\' ),
        \'add_new_item\'      => __( \'Add New Team_Category\' ),
        \'new_item_name\'     => __( \'New Team_Category Name\' ),
        \'menu_name\'         => __( \'Team_Category\' ),
    );

    $args = array(
        \'hierarchical\'      => true,
        \'labels\'            => $labels,
        \'show_ui\'           => true,
        \'show_admin_column\' => false,
        \'query_var\'         => true,
        \'rewrite\'           => array( \'slug\' => \'Team_Category\' ),
    );

    register_taxonomy( \'Team_Category\', array( \'team\' ), $args );
}
/****************************taxanomy end***********************************/
但是,当我在WP\\u查询中使用tax\\u查询时,我不会得到任何帖子。这是我的密码

<?php
$taxonomy_name = \'Team_Category\';
$get_categories = get_terms($taxonomy_name);
$total_categories = count($get_categories);
// Loop through the obituaries:
for ($i = 0; $i < $total_categories; $i++) {
    ?>
    <div class="row">
        <div class="col-md-4">
            <?php echo $category_name = $get_categories[$i]->name; ?>
        </div>
        <?php
        $args = array(
            \'post_type\' => \'team\',
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'Team_Category\', 
                    \'field\' => \'slug\', \'terms\' => $category_name,)
                )
         );

        $query = new WP_Query($args);

        if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            the_title();
        }
        }
        wp_reset_query(); ?>

    </div>

<?php }
它在没有tax_query.我做了很多谷歌搜索,但没有找到合适的结果。任何解决此问题的解决方案。任何帮助都将不胜感激

1 个回复
SO网友:Krzysiek Dróżdż

我认为这个问题在您的代码中是正确的,但很容易被忽视。让我们仔细看看这一部分:

    <div class="col-md-4">
        <?php echo $category_name = $get_categories[$i]->name; ?>
    </div>
    <?php
    $args = array(
        \'post_type\' => \'team\',
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'Team_Category\', 
                \'field\' => \'slug\', \'terms\' => $category_name
            )
        )
    );
这里到底发生了什么?您可以获取术语的名称并将其存储为$category_name ($category_name = $get_categories[$i]->name;).

稍后,将此值用作slug(\'field\' => \'slug\', \'terms\' => $category_name).

Slug和name是不同的东西,所以您的Tax\\u查询很可能找不到任何东西(如果name包含大写字母、空格或任何特殊字符,Slug将与name不同)。

因此,如果您将其更改为如下所示,它应该可以正常工作:

    <div class="col-md-4">
        <?php echo $get_categories[$i]->name; ?>
    </div>
    <?php
    $args = array(
        \'post_type\' => \'team\',
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'Team_Category\', 
                \'field\' => \'slug\',
                \'terms\' => $get_categories[$i]->slug;
            )
        )
    );

    $query = new WP_Query($args);