Post Views Counter插件还是蛮好用的,可以在主页和单页面显示文章的浏览量。如果想显示其他信息的话,就需要自己修改插件了。自己在原代码上修改了下,可显示文章评论数和发布日期。
具体修改如下:
1.打开wp-content\plugins\post-views-counter\includes\functions.php文件;
2.修改pvc_post_views函数,代码如下:
if ( ! function_exists( 'pvc_post_views' ) ) {
function pvc_post_views( $post_id = 0, $display = true ) {
// get all data
$post_id = (int) ( empty( $post_id ) ? get_the_ID() : $post_id );
// get display options
$options = Post_Views_Counter()->options['display'];
// get term views
$views = pvc_get_post_views( $post_id );
// container class
$class = apply_filters( 'pvc_post_views_class', 'post-views content-post post-' . $post_id . ' entry-meta', $post_id );
// prepare display
$label = apply_filters( 'pvc_post_views_label', ( function_exists( 'icl_t' ) ? icl_t( 'Post Views Counter', 'Post Views Label', $options['label'] ) : $options['label'] ), $post_id );
// get icon class
$icon_class = ( $options['icon_class'] !== '' ? esc_attr( $options['icon_class'] ) : '' );
// add dashicons class if needed
$icon_class = strpos( $icon_class, 'dashicons ' ) === 0 ? $icon_class : 'dashicons ' . $icon_class;
// prepare icon output
$icon = apply_filters( 'pvc_post_views_icon', '<span class="post-views-icon ' . $icon_class . '"></span> ', $post_id );
//start---添加的代码
$comment_count_arr = get_comment_count($post_id);
$comment_count_html = '';
$separator = ' / ';
if($comment_count_arr['total_comments'] > 0){
$comment_count_html = $separator.'评论:'.$comment_count_arr['total_comments'];
}
$post_add_date = get_the_date();
$post_add_date_html = $separator.$post_add_date;
//end---添加的代码
//start--修改的代码
$html = apply_filters(
'pvc_post_views_html',
'<div class="' . esc_attr( $class ) . '">
' . ( $options['display_style']['icon'] && $icon_class !== '' ? $icon : '' )
. ( $options['display_style']['text'] && $label !== '' ? '<span class="post-views-label">' . esc_html( $label ) . '</span> ' : '' )
. '<span class="post-views-count">' . number_format_i18n( $views ) . $comment_count_html.$post_add_date_html.'</span>
</div>',
$post_id,
$views,
$label,
$icon
);
//end--修改的代码
if ( $display )
echo $html;
else
return $html;
}
}
未修改前只显示浏览量,修改后效果如上图。