我有一个名为'staff'的自定义帖子类型 . 在此我有一个名为'team_name'的分类 . 目前我已经增加了一个名为Corporate&Commercial Team的团队(标签6,slug corporate-commercial-team)
这是模板中的代码:
<?php
$args = array(
'post_type' => 'staff',
'tax_query' => array(
array(
'taxonomy' => 'team_name',
'field' => 'corporate-commercial-team',
'terms' => 6
)
)
);
$staffs = new WP_Query( $args );
if( $staffs->have_posts() ) {
while( $staffs->have_posts() ) {
$staffs->the_post();
?>
<h1><?php the_title() ?></h1>
<?php
}
}
else {
echo 'Oh oh no products!';
}
?>
这是我的函数代码:
function team_name() {
$labels = array(
'name' => _x( 'Team Name', 'taxonomy general name' ),
'singular_name' => _x( 'Team Name', 'taxonomy singular name' ),
'search_items' => __( 'Search Teams' ),
'all_items' => __( 'All Teams' ),
'parent_item' => __( 'Parent Team' ),
'parent_item_colon' => __( 'Parent Team:' ),
'edit_item' => __( 'Edit Team' ),
'update_item' => __( 'Update Team' ),
'add_new_item' => __( 'Add New Team' ),
'new_item_name' => __( 'New Team' ),
'menu_name' => __( 'Teams' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'team_name', 'staff', $args );
}
add_action( 'init', 'team_name', 0 );
我已经重新保存了固定链接,并将此类别分配给其中一个帖子但我得到'哦,不,没有产品!'
有什么建议吗?
1 回答
您对
tax_query
中的参数存在误解 .field
参数是应搜索的特定列,列名称应与传递给terms
参数的实际术语值相对应field
的默认值为term_id
. 每当省略此参数或将其设置为term_id
时,terms
应为术语 ID's 的数组或字符串 .field
的其他值是slug
和name
,因此您应该将一个术语段或名称的数组或字符串分别传递给terms
参数你的
tax_query
应该是