调用WordPress 文章标签[带文章数统计]

我们都知道the_tagsget_the_tags可以调用文章标签,但也无非就是调用标签,其实每个标签都包含了很多参数,只调用名字和链接有点太浪费了,所以我们在加上一个小小的文章数统计,瞬间变的高大上起来。

实现方法

下面的代码加到functions.php中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    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中使用下面代码即可:

1
<?php echo fa_get_the_term_list( get_the_ID(), 'post_tag' );?>
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

请我喝杯咖啡吧~

支付宝
微信