分类类别固定链接

Taxonomy Category Permalink

我正在开发自定义 post 类型,该类型使用名为 "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>';
    }
?>

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

对于其他想知道的人,标记是:

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