有没有更好的方法可以在wordpress中获取自定义文章类型的类别名称?

<?php        // get the portfolio categories
            $terms = get_the_terms( $post->ID, 'filters' );
            if ( $terms && ! is_wp_error( $terms ) ) :
                $names = array();
                $slugs = array();
                foreach ( $terms as $term ) {
                 $names[] = $term->name;
                 $slugs[] =  $term->slug;
                }
                $name_list = join( " / ", $names );
                $slug_list = join( " category-", $slugs );
        endif;
    ?>


    <!-- BEGIN portfolio-item-->
    <li class="portfolio-item third column category-<?php echo $slug_list; ?>" data-filter="category-<?php echo $slug_list; ?>">

最佳答案

<?php
$taxonomy = 'filters';
$terms = get_terms($taxonomy);

if ( $terms && !is_wp_error( $terms ) ) :
?>
    <ul>
        <?php foreach ( $terms as $term ) { ?>
            <li><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li>
        <?php } ?>
    </ul>
<?php endif;?>

或者在fuctions.php中放置:
  function get_the_category_custompost( $id = false, $tcat = 'category' ) {
    $categories = get_the_terms( $id, $tcat );
    if ( ! $categories )
        $categories = array();

    $categories = array_values( $categories );

    foreach ( array_keys( $categories ) as $key ) {
        _make_cat_compat( $categories[$key] );
    }

    return apply_filters( 'get_the_categories', $categories );
}

并将函数调用为:
<?php $cat = get_the_category_custompost($post->ID, 'Your Custom Taxonomy'); ?>

07-24 20:31