使用参数

时间:2015-04-22 10:33:23

标签: wordpress custom-post-type args

我有一个名为'staff'的自定义帖子类型。在此我有一个名为'team_name'的分类。目前我已经添加了一个名为Corporate& amp;的团队。商业团队(标签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 个答案:

答案 0 :(得分:0)

您对tax_query内的参数存在误解。 field参数是应搜索的特定列,列名称应与传递给terms参数的实际术语值相对应

field的默认值为term_id。每当省略此参数或将其设置为term_id时,terms应为术语 ID 的数组或字符串。 field的其他值为slugname,因此您应该将一个数组或字符串段落或名称分别传递给terms参数

您的tax_query应该是

  'tax_query' => array(
    array(
      'taxonomy' => 'team_name',
      'field' => 'term_id',
      'terms' => 6
    )
  )
相关问题