【发布时间】:2016-01-29 17:26:35
【问题描述】:
我正在从事插件开发,我的插件名称是 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_Query 中使用 tax_query 时,我没有收到任何帖子。 这是我的代码
<?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,它可以完美运行。我做了很多谷歌,但没有找到合适的结果。解决这个问题的任何解决方案。任何帮助将不胜感激
【问题讨论】:
-
post-status 不应该是 post_status?
-
顺便说一句,publish 是 post_status 的默认值,所以不需要添加它
-
@ThemesCreator 我现在删除了
'post-status'=>'publish',但它对结果没有影响 -
你有没有测试变量$category_name的值是否正确?
-
@ThemesCreator ya 当我使用
<?php echo $category_name = $get_categories[$i]->name; ?>时,它给出了名称,因为有两个名称..
标签: custom-post-type wordpress custom-taxonomy