WordPress技巧:带有文章统计数的标签
文章目录[隐藏]
- 实现方法
- 调用方法
copy 自大发贱志,站点已使用,效果见本文下端标签。
实现方法
将下列代码放置到主题模板函数 functions.php
闭合中:
//标签-带有文章统计
function fa_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_links .= '<a href="' . esc_url( $link ) . '" class="js-loaded post--keyword" data-title="' . $term->name . '" data-type="'. $taxonomy .'" data-term-id="' . $term->term_id . '">' . $term->name . '<sup>['. $term->count .']</sup></a>';
}
return $term_links;
}
调用方法
在 loop 中使用下面代码即可:
<?php echo fa_get_the_term_list( get_the_ID(), 'post_tag' );?>