wordpress,获取自定义帖子类型的类别名称

时间:2013-11-25 15:22:37

标签: php html css wordpress

有没有更好的方法来获取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; ?>">

3 个答案:

答案 0 :(得分:15)

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

或者在functions.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'); ?>

答案 1 :(得分:4)

我的回答似乎太简单了,但是我用它来列出一个名为DW Question Answer的wordpress插件中的类别,该插件与标准wp类别有不同的类别。

我假设Design Wall使用自定义帖子类型来创建q&amp; a part和分类以创建类别。

<ul>
  <?php wp_list_categories('taxonomy=dwqa-question_category&hide_empty=0&orderby=id&title_li=');?>
</ul>

答案 2 :(得分:0)

假设您的自定义分类是recipegroups,我已经在functions.php中实现并测试了这段代码,我相信它也适用于插件。

$recipeTerms = get_terms(array( 
    'taxonomy' => 'recipegroups',

));
        foreach($recipeTerms as $recipeTerm){
            if($recipeTerm->parent==0){
                echo "<div class='termsBox'>"; // remove these div's to suit your needs..

                $termLink =get_term_link( $recipeTerm );
                echo "<a href='$termLink'><div class='termParent'>".$recipeTerm->name."</div></a>  ";

                $termChilds = get_term_children($recipeTerm->term_id, 'recipegroups' );
                foreach($termChilds as $child){
                    $chTerm = get_term_by( 'id', $child, 'recipegroups');
                    $termLink =get_term_link( $chTerm );
                    echo "<a href='$termLink'><div class='top-cat-items'>".$chTerm->name."</div></a>";
                    }
                    echo "</div>"; // end of termsBox div
                }

            }
相关问题