在存档页面中获取自定义帖子类型的类别ID

时间:2016-09-15 15:16:42

标签: wordpress

我有这样的自定义分类和类别:

products_categories
    cat-1
         cat-1 
              post-1   post-2    post-3
         cat-2 
              post-1   post-2    post-3
         cat-3 
              post-1   post-2    post-3
    cat-2
         cat-1 
              post-1   post-2    post-3
         cat-2 
              post-1   post-2    post-3
         cat-3 
              post-1   post-2    post-3

我想将存档页面设为:

  • 查询该自定义分类的所有类别;
  • 然后查询这些类别的所有帖子。

这是我的档案:

首先:

$args = array(
'type'              => 'products',       
'taxonomy'          => 'products_categories',    
$categories = get_categories( $args );

第二

<? foreach ( $categories as $category ):
$cat_id = $category->term_id;
$cat_name = $category->cat_name;
?>

第三

<?php
$cats_id= array();
foreach ( $sub_categories as $sub_category ) :
    $cats_id[]=$sub_category->term_id;
endforeach;

$args = array(
 'posts_per_page' => -1,
 'orderby' => 'rand',
 'post_type' => 'products',
 'post_status' => 'publish',
 'tax_query' => array(
   array(
   'taxonomy' => 'products_categories',
   'field' => 'id',
   'terms' => $cats_id,
   ),
));

$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
?>

没关系。我得到父类别的那些子类别的所有帖子;

现在我想在上一节中获取帖子的类别ID。

1 个答案:

答案 0 :(得分:0)

当前帖子的类别是:

get_the_terms()

这将返回附加到帖子的类别数组。

相关问题