统计 Wordpress 文章阅读数
**WP-PostViews Plus **插件提供了统计文章阅读数的功能,与其他的插件不同的是,我们不能通过简单的配置使其生效。它仅提供一个the_views()
函数,我们需要在哪里展示阅读数,就在哪里调用the_views()
函数。
在我使用的主题中,这个位置原本展示的是作者名:
我想把作者名字替换掉,展示文章的阅读数。顺藤摸瓜,我找到了相关的函数:
function nisargpro_print_post_author_name() {
// $show_author_name = get_theme_mod( 'nisargpro_display_author_setting', true );
// $viewbyauthor_text = __( 'View all posts by', 'nisargpro' ).' %s';
// $author_by_line = '';
// if( $show_author_name ) {
// $author_by_line = '<span class="byline"><span class="sep"></span><i class="fa fa-user"></i> <span class="author vcard"><a class="url fn n" href="%s" title="%s" rel="author">%s</a></span></span>';
// $author_by_line = sprintf($author_by_line,
// esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
// esc_attr( sprintf( $viewbyauthor_text, get_the_author() ) ),
// esc_html( get_the_author() ));
// print $author_by_line; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
// }
if(function_exists('the_views')) {
print '<i class="fa fa-user"></i>';
print ' ';
print the_views();
}
}
把源代码注释掉,把我自己的代码搞上去,大功告成。