最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can i show post views using specified post ID?

programmeradmin1浏览0评论

Here is the code...

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;
}
}

it works fine using this php code to show views <?php mvp_post_views(); ?>

Thou would like to show views using specified posts ID...

Here is the code...

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;
}
}

it works fine using this php code to show views <?php mvp_post_views(); ?>

Thou would like to show views using specified posts ID...

Share Improve this question asked May 31, 2019 at 0:48 John4PeaceJohn4Peace 31 bronze badge 1
  • 1 if ($count=='') should be if (!$count) as get_post_meta returns false not an empty string if the key is not set. otherwise seems okay. – majick Commented May 31, 2019 at 4:08
Add a comment  | 

1 Answer 1

Reset to default 0

You can pass post id as argument in the views function. Please check following example. An optional argument is passed in the function. If nothing is passed, id is fetched from 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;
    }
}
发布评论

评论列表(0)

  1. 暂无评论