web-dev-qa-db-fra.com

Comment améliorer l'affichage du nombre de vues de publication?

Je voulais savoir si je pouvais changer la façon dont j’affichais les vues dans WordPress.

Exemple: 1000 vues = 1k - 10000 vues = 10k

Je compte et visualise les vues des publications à l'aide de ce code:

// 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());
  }
}
1
Hank Scorpio

Oui, vous pouvez. Vous devez vérifier si le nombre de vues de publication est supérieur à 1000, si, puis contournez-le et renvoyez-le:

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';
    }
}
1
Jack Johansson

J'utilise cette fonction pendant un moment et fonctionne très bien pour mes besoins:

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

maintenant, vous appelez simplement la fonction comme dans cet exemple:

$views = getPostViews($postID);     
$views = short_number($views);
return $views;

J'espère que cela aidera quelqu'un d'autre dans le besoin: D

0
jrxpress