如何提高帖子点击量的显示?

时间:2017-06-10 作者:Hank Scorpio

我想知道我是否可以改变WordPress中显示视图计数的方式。

示例:1000个视图=1k-10000个视图=10k

我正在使用以下代码计算和查看帖子视图:

// Count views
function setPostViews($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);
  }
}

// Show views
function getPostViews($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\';
}

// Show views in WP-Admin
add_filter(\'manage_posts_columns\', \'posts_column_views\');
add_action(\'manage_posts_custom_column\', \'posts_custom_column_views\', 5, 2);
function posts_column_views($defaults) {
  $defaults[\'post_views\'] = __(\'Views\');
  return $defaults;
}
function posts_custom_column_views($column_name, $id){
  if($column_name === \'post_views\') {
    echo getPostViews(get_the_ID());
  }
}

2 个回复
最合适的回答,由SO网友:Johansson 整理而成

是的,你可以。您必须检查后视图计数是否大于1000,是否大于1000,然后将其四舍五入并返回:

function getPostViews($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";
    }
    if ($count > 1000) {
        return round ( $count / 1000 ,1 ).\'K Views\';
    } else {
        return $count.\' Views\';
    }
}

SO网友:jrxpress

我使用此功能有一段时间,非常适合我的需要:

/**
 * Shorten long numbers to K/M/B (Thousand, Million and Billion)
 *
 * @param int $number The number to shorten.
 * @param int $decimals Precision of the number of decimal places.
 * @param string $suffix A string displays as the number suffix.
 */
if(!function_exists(\'short_number\')) {
function short_number($n, $decimals = 2, $suffix = \'\') {
    if(!$suffix)
        $suffix = \'K,M,B\';
    $suffix = explode(\',\', $suffix);

    if ($n < 1000) { // any number less than a Thousand
        $shorted = number_format($n);
    } elseif ($n < 1000000) { // any number less than a million
        $shorted = number_format($n/1000, $decimals).$suffix[0];
    } elseif ($n < 1000000000) { // any number less than a billion
        $shorted = number_format($n/1000000, $decimals).$suffix[1];
    } else { // at least a billion
        $shorted = number_format($n/1000000000, $decimals).$suffix[2];
    }

    return $shorted;
}
}
现在您只需调用如下示例中的函数:

$views = getPostViews($postID);     
$views = short_number($views);
return $views;
我希望它能帮助其他需要帮助的人:D

结束

相关推荐

Making post views as sortable

我正在wordpress管理帖子页面上使用自定义字段,添加以下代码,然后尝试根据帖子视图对帖子进行排序,但排序不起作用。下面是我在函数中添加的代码。php:add_filter(\'manage_posts_columns\', \'posts_column_views\'); add_action(\'manage_posts_custom_column\', \'posts_custom_column_views\',5,2); function posts_column_views(