这可以通过使用get_the_tags()
按排序结果count
每个标记的属性usort()
, 然后通过它们循环并将其作为链接输出:
/*
* Get array of tags and sort by count.
*/
$tags = get_the_tags();
if ( $tags && ! is_wp_error( $tags ) ) {
usort( $tags, function( $a, $b ) {
return $b->count - $a->count; // Swap $a and $b for ascending order.
} );
/*
* Get an HTML link for each tag and put in an array.
*/
$links = array();
foreach ( $tags as $tag ) {
$links[] = \'<a href="\' . esc_url( get_tag_link( $tag ) ) . \'" rel="tag">\' . $tag->name . \'</a>\';
}
/*
* Output links separated by comma.
*/
echo implode( \', \', $links );
}
另一个实现是创建
the_tags()
, 将标记重新排序为
wp_list_sort()
通过
get_the_terms
过滤器:
/**
* Retrieve the tags for a post, ordered by post count.
*
* @param string $before Optional. Before list.
* @param string $sep Optional. Separate items using this.
* @param string $after Optional. After list.
*/
function the_tags_wpse( $before = null, $sep = \', \', $after = \'\' ) {
add_filter( \'get_the_terms\', $callback = function( $tags ) {
return wp_list_sort( $tags, \'count\', \'desc\' );
}, 999 );
the_tags( $before, $sep, $after );
remove_filter( \'get_the_terms\', $callback, 999 );
}