wordpress,获取自定义职位类型的类别名称 [英] wordpress, get category names for a custom post type

查看:173
本文介绍了wordpress,获取自定义职位类型的类别名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有更好的方法来获取自定义的postpress类型的类别名称?

Is there a better way to get the category names for a custom post type in 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中:

Or in fuctions.php place this:

  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'); ?>

这篇关于wordpress,获取自定义职位类型的类别名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆