首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取存档页面中自定义post类型的类别id

获取存档页面中自定义post类型的类别id
EN

Stack Overflow用户
提问于 2016-09-15 15:16:42
回答 1查看 3K关注 0票数 0

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

代码语言:javascript
复制
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

我想创建一个存档页面:

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

这是我的档案:

第一:

代码语言:javascript
复制
$args = array(
'type'              => 'products',       
'taxonomy'          => 'products_categories',    
$categories = get_categories( $args );

第二:

代码语言:javascript
复制
<? foreach ( $categories as $category ):
$cat_id = $category->term_id;
$cat_name = $category->cat_name;
?>

第三:

代码语言:javascript
复制
<?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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-15 15:43:59

目前员额的职类如下:

代码语言:javascript
复制
get_the_terms()

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39514703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档