分类法类别永久链接

时间:2017-12-31 15:39:55

标签: php wordpress taxonomy

我正在使用一种名为" portfolio-category"的自定义分类法的自定义帖子类型。

现在我正在尝试列出此分类法下的类别 - 使用相关的永久链接。

列出它们很好 - 似乎无法找到显示固定链接的正确术语(目前在下面的代码中标注为#)。

这是我的代码:

<?php
    // your taxonomy name
    $tax = 'portfolio-category';

    // get the terms of taxonomy
    $terms = get_terms( $tax, [
        'hide_empty' => true, // do not hide empty terms
    ]);

    // loop through all terms
    foreach( $terms as $term ) {

        // if no entries attached to the term
        if( 0 == $term->count )
        echo '<li><a href="#">' .$term->name. '</a></li>';

        // if term has more than 0 entries
        elseif( $term->count > 0 )
        echo '<li><a href="#">' .$term->name. '</a></li>';
    }
?>

2 个答案:

答案 0 :(得分:0)

终于找到答案了(发帖后很烦人 - 抱歉)。

对于其他想要了解的人,标记是:

<?php
    // your taxonomy name
    $tax = 'portfolio-category';

    // get the terms of taxonomy
    $terms = get_terms( $tax, [
         'hide_empty' => true, // do not hide empty terms
    ]);

    // loop through all terms
    foreach( $terms as $term ) {

        $term_link = get_term_link( $term );

        // if no entries attached to the term
        if( 0 == $term->count )
        echo '<li><a href="' .esc_url( $term_link ). '">' .$term->name. '</a></li>';

        // if term has more than 0 entries
        elseif( $term->count > 0 )
        echo '<li><a href="' .esc_url( $term_link ). '">' .$term->name. '</a></li>';
    }
?>

答案 1 :(得分:0)

<?php   $args = array(
    'taxonomy' => 'portfolio-category', // your taxonomy name
    'hide_empty' => true,
    );

// get the terms of taxonomy
$terms = get_terms( $args ); // Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args

// loop through all terms
if( is_array( $terms ) && count( $terms ) > 0 ){
    $html = '';
    foreach( $terms as $term ) {

        $term_link = get_term_link( $term );
        $html .= '<li><a href="' .esc_url( $term_link ). '">' .esc_html( $term->name ) . '</a></li>';
    }
    echo $html;
} ?>