循环遍历分类及其术语(新手问题)

时间:2018-01-23 作者:Boiledfrog

我有一个名为“classe”的自定义帖子类型。我正在尝试遍历这些帖子,以获得它们的分类法和术语列表。这是我的代码:

$type = \'classe\';
    $args = array(
        \'post_type\' => $type,
        \'post_status\' => \'publish\',
        \'posts_per_page\' => -1,
        \'ignore_sticky_posts\'=> true
    );

    $loop = null;
    $loop = new WP_Query($args);

    if( $loop->have_posts() ):
        while ($loop->have_posts()) : $loop->the_post();

            $taxonomies = ???? //Get taxonomies

            $result = \'<div class="col-xs-12 col-md-4">\';
                $result .= \'<div class="grid-content">\';
                $result .= \'<h2>\' . get_the_title() .\'</h2>\';
                $result .= \'<p>\' . get_the_excerpt() .\'</p>\';
                foreach($taxonomies as $taxonomy){
                    $terms = ???? //Get terms
                    $result .= \'<h4>\' . $taxonomy . \'</h4>\';
                    foreach($terms as $term){
                        $result .= \'<p>\' . $term . \'</p>\';
                    }
                }
                $result .= \'</div>\';
            $result .= \'</div>\';
            echo $result;
        endwhile;
    endif;
    wp_reset_query();

1 个回复
SO网友:Chin Leung

您可以使用该功能wp_get_post_terms 检索您循环浏览的帖子的条款。

$taxonomies = wp_get_post_terms(get_the_ID(), \'taxonomy-name\');

结束