这是密码。。。
if ( !function_exists( \'getCrunchifyPostViews\' ) ) {
function getCrunchifyPostViews($postID){
$count_key = \'post_views_count\';
$count = get_post_meta($postID, $count_key, true);
if($count==\'\'){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, \'0\');
return "0 View";
}
return $count.\' Views\';
}
}
if ( !function_exists( \'setCrunchifyPostViews\' ) ) {
function setCrunchifyPostViews($postID) {
$count_key = \'post_views_count\';
$count = get_post_meta($postID, $count_key, true);
if($count==\'\'){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, \'0\');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
}
if ( !function_exists( \'mvp_post_views\' ) ) {
function mvp_post_views(){
$post_id = get_the_ID();
$count_key = \'post_views_count\';
$n = get_post_meta($post_id, $count_key, true);
if ($n > 999999999) {
$n_format = number_format($n / 1000000000, 1) . \'B\';
} else if ($n > 999999) {
$n_format = number_format($n / 1000000, 1) . \'M\';
} else if ($n > 999) {
$n_format = number_format($n / 1000, 1) . \'K\';
} else {
$n_format = $n;
}
echo $n_format;
}
}
使用此php代码显示视图效果很好
<?php mvp_post_views(); ?>
您想使用指定的帖子ID显示视图。。。
最合适的回答,由SO网友:Nilambar Sharma 整理而成
您可以在views函数中将post id作为参数传递。请检查以下示例。在函数中传递可选参数。如果未传递任何内容,则从get_the_ID()
.
if ( !function_exists( \'mvp_post_views\' ) ) {
function mvp_post_views( $post_id = \'\' ){
if ( $post_id ) {
$post_id = absint( $post_id );
} else {
$post_id = get_the_ID();
}
$count_key = \'post_views_count\';
$n = get_post_meta($post_id, $count_key, true);
if ($n > 999999999) {
$n_format = number_format($n / 1000000000, 1) . \'B\';
} else if ($n > 999999) {
$n_format = number_format($n / 1000000, 1) . \'M\';
} else if ($n > 999) {
$n_format = number_format($n / 1000, 1) . \'K\';
} else {
$n_format = $n;
}
echo $n_format;
}
}