2016-09-15 49 views
0

我有自定义分类和类别是这样的:获取自定义后类型的类别编号存档页面

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。

+0

你想要最接近的分类层次或顶级类别? – samisonline

回答

0

当前后的类别将是:

get_the_terms() 

这返回附连到柱类别的阵列。

+0

Sam .. thx ...你做了我的一天。 – behzad

+0

如果你不知道每一个,那么有一堆类似的wordpress调用看起来是一样的。 https://developer.wordpress.org/reference/functions/get_the_terms/是一个很好的网站来浏览!如果你可以标记我的答案是正确的,如果它有帮助,那将是非常棒的! – samisonline

相关问题