显示带有链接的分类术语

时间:2021-01-24 作者:pasquale

如何打印带有链接的分类术语??此代码可以工作,但打印条款时没有链接。。一些建议??

function show_all_terms($taxonomy){
    $taxonomy_terms = get_terms($taxonomy);
    foreach($taxonomy_terms as $term){
        $terms[] = $term->name;
    }
    if(!empty($terms)) {
        echo join(", ", $terms);
    }
    else{
        echo "No ".$taxonomy."associated with this ".get_post_type();
    }
}

2 个回复
SO网友:Rup

您需要使用get_term_link(), e、 g.将术语或链接的$terms数组改为HTML:

    foreach( $taxonomy_terms as $term ) {
        $term_link = get_term_link( $term->slug, $taxonomy );
        if (! is_wp_error( $term_link ) ) {
            $terms[] = \'<a href="\' . esc_url( $term_link ) . \'">\' . esc_html( $term->name )
                       . \'</a>\';
        } else {
            // Could not fetch link for this term
            $terms[] = esc_html( $term->name );
        }
    }

SO网友:HK89

根据需要使用此函数列出所有分类术语和链接

  function get_term_list($taxonomy){
    
       $terms = get_terms($taxonomy);
     
       echo \'<ul class="list-cat">\';
     
          foreach ( $terms as $term ) {
     
           // The $term is an object, so we don\'t need to specify the $taxonomy.
           $term_link = get_term_link( $term );
        
            // If there was an error, continue to the next term.
           if ( is_wp_error( $term_link ) ) {
               continue;
           }
     
           // We successfully got a link. Print it out.
           echo \'<li><a href="\' . esc_url( $term_link ) . \'">\' . $term->name . \'</a></li>\';
    } 
   echo \'</ul>\';

   }
并包括(&A);在管理面板中为您的页面使用此短代码

[all\\u get\\u term\\u list分类法=“category”]

默认情况下,这个短码使用的分类法是category

add_shortcode ( \'all_get_term_list\', \'all_get_term_list\' );

function all_get_term_list($atts){

$a = shortcode_atts( array(
        \'taxonomy\' => \'category\'
        ), $atts );

$terms = get_terms($a[\'taxonomy\']);
 
$output.= \'<ul class="list-cat">\';
 
foreach ( $terms as $term ) {
 
    // The $term is an object, so we don\'t need to specify the $taxonomy.
    $term_link = get_term_link( $term );
    
    // If there was an error, continue to the next term.
    if ( is_wp_error( $term_link ) ) {
        continue;
    }
 
    // We successfully got a link. Print it out.
    $output.= \'<li><a href="\' . esc_url( $term_link ) . \'">\' . $term->name . \'</a></li>\';
}
 
$output.= \'</ul>\';

return $output;

}

相关推荐

Filter Custom Taxonomy Posts

我有一个自定义的分类页面,它也有一个向上的滑块。问题是滑块显示来自所有分类法而非当前分类法的随机帖子。是否有办法过滤滑块中的图像,使其仅使用当前分类法?<?php global $taxonomy_location_url, $taxonomy_profile_url; $args = array( \'post_type\' => $taxonomy_profile_url, \'orderby\' => \'rand\', \'meta_que