如何在自定义POST_TYPE循环中呈现分类

时间:2015-03-15 作者:Ryan

我自定义了帖子类型和分类法,在我的页面模板上,我想在css类中呈现分类法名称。有人知道如何渲染吗?

这就是我目前所拥有的

<?php
        $args = array( \'post_type\' => \'portfolio\', \'posts_per_page\' => 10 );
        $loop = new WP_Query( $args );

        while ( $loop->have_posts() ) : $loop->the_post();
          $terms = get_the_terms( get_the_ID(), \'portfolio\' );
          //debugging array
          echo \'<pre>\';
          print_r($terms);
          echo \'</pre>\';
          echo "<a href=\'" .get_the_permalink() . "\'><figure class=\'". $terms->name ."\'>";
          the_post_thumbnail();
          echo "</figure></a>";
        endwhile;
    ?>
结果是:

Array
(
    [200] => stdClass Object
        (
            [term_id] => 200
            [name] => Plugins
            [slug] => plugins
            [term_group] => 0
            [term_taxonomy_id] => 200
            [taxonomy] => portfolio
            [description] => 
            [parent] => 0
            [count] => 1
            [object_id] => 1776
            [filter] => raw
        )

)
我试着这样称呼它:

$terms->name;
但我在wp中得到了以下信息:

Notice:  Trying to get property of non-object in....

1 个回复
SO网友:Mostafa Norzade

try this code :

foreach ($terms as $term) {
   echo $term->name; 
   echo $term->slug;

   ...
   ... 
}
结束

相关推荐

高级或不高级的PRE_GET_POSTS查询

我只想显示那些有“现在和未来”孩子使用pre_get_posts 行动挂钩。有人知道如何做到这一点吗?function pre_get_posts($query) { if(!is_admin() && $query->is_main_query()) { //check if the query matches this post type if(is_post_type_archive(\'event