2016-11-01 56 views
0

中的自定义帖子类型和类别,因此我试图输出自定义帖子类型的服务和特定类别的帖子的名称。为什么我不能循环访问WordPress

不过我用的就是输出所有文章类型

$args = array(
        'post_type' => 'service', 
        'cat' => $cat 
       ); 

       $query = new WP_Query($args); 

       echo '<pre>'; 
       print_r($query); 
       echo '</pre>'; 

      ?> 

      <div id="subCatBox"> 
       <ul id="subCatlist" class="list"> 

        <?php if($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?> 

         <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 

        <?php endwhile; else : ?> 
        <?php endif; ?> 

       </ul> 
      </div> 

这里的类别代码的查询对象的输出

WP_Query Object 
(
[query] => Array 
    (
     [post_type] => service 
     [cat] => 4 
    ) 

[query_vars] => Array 
    (
     [post_type] => any 
     [cat] => 4 
     [error] => 

我不明白为什么在查询post_type是service,但是在query_vars中是any。任何帮助将大规模赞赏

+0

尝试get_the_category(int $ id = false)来获取帖子类别。 – user3040610

+0

我已经得到这样的类别$ cat = get_the_category()[0] - > cat_ID; – Mike

回答

0

我已经解决了它。以防万一这可以帮助其他人使用category__in代替cat

$query = new WP_Query(array(
    'post_type' => 'post', 
    'category__in' => $cat 
));