首页 文章

使用args显示自定义帖子类型分类

提问于
浏览
-1

我有一个名为'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 回答

  • 0

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

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

    你的 tax_query 应该是

    'tax_query' => array(
        array(
          'taxonomy' => 'team_name',
          'field' => 'term_id',
          'terms' => 6
        )
      )
    

相关问题