Post_tag按文章数排序输出

   

WordPress一个常用的字符数组是post_tag,可以用来显示标签。

如果只是想单纯按文章数排序列出所有的标签,可以用内置的wp_tag_cloud

<?php wp_tag_cloud('number=1000&orderby=count'); ?>

如果想要按文章数排序输出当前文章的标签并自定义显示方式,则需要用到array_multisort()函数:

function s_get_the_term_list( $id, $taxonomy ) {
    $terms = get_the_terms( $id, $taxonomy );
    $term_links = "";
    if ( is_wp_error( $terms ) )
        return $terms;
    if ( empty( $terms ) )
        return false;
    foreach ( $terms as $term ) {
        $link = get_term_link( $term, $taxonomy );
        if ( is_wp_error( $link ) )
            return $link;
	$term_count[] = $term->count;
	$term_link[] = '<a href="' . esc_url( $link ) . '" class="post--keyword" data-title="' . $term->name . '" data-type="'. $taxonomy .'" data-term-id="' . $term->term_id . '">' . $term->name . '<sup>['. $term->count .']</sup></a>';
    }
   array_multisort($term_count,SORT_DESC,$term_link);
   foreach ( $term_link as $tlink) {
	$term_links .= $tlink;
   }
   return $term_links;
}

调用方式:

<?php echo s_get_the_term_list( get_the_ID(), 'post_tag' );?>

共有 0 条评论

           

    发表评论

    您的电子邮箱地址不会被公开。 必填项已用 * 标注