WordPress文章如何添加浏览次数

在主题文件夹下面的inc文件夹(如果没有就新建一个),新建一个postviews.php文件,然后在functions.php里面引入:

require get_template_directory() . '/inc/postviews.php'

然后在文件里粘贴:

<?php
// function to display number of posts.
// 可以在后台设置一个option来决定是否开启浏览次数 
function tie_views( $text ='' , $postID ='' ){
  global $post;

  if( empty($postID) ){
    $postID = $post->ID ;
  }
  
    $count_key  = 'tie_views';
    $count    = (int)get_post_meta($postID, $count_key, true);
  $count    = @number_format($count);
    if( empty($count) ){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, 0 );
        $count = 0;
    }
    return '<span class="post-views"><i class="fa fa-eye"></i>'.$count.' '.$text.'</span> ';
}

// function to count views.
function tie_setPostViews() {
  global $post, $page;

  if( $page > 1  ){
    return false;
  }

  $count    = 0;
  $postID   = $post->ID ;
    $count_key  = 'tie_views';
    $count    = (int)get_post_meta($postID, $count_key, true);

  if( !defined('WP_CACHE') || !WP_CACHE ){
    $count++;
    update_post_meta($postID, $count_key, (int)$count);
  }
}

### Function: Calculate Post Views With WP_CACHE Enabled
add_action('wp_enqueue_scripts', 'tie_postview_cache_count_enqueue');
function tie_postview_cache_count_enqueue() {
  global $post;
  if ( is_single() && ( defined('WP_CACHE') && WP_CACHE) ) {
    // Enqueue and localize script here
    wp_enqueue_script( 'tie-postviews-cache', get_template_directory_uri() . '/js/post_view.js', array( 'jquery' ) );
    wp_localize_script( 'tie-postviews-cache', 'tieViewsCacheL10n', array('admin_ajax_url' => admin_url('admin-ajax.php', (is_ssl() ? 'https' : 'http')), 'post_id' => intval($post->ID)));
  }
}

### Function: Increment Post Views
add_action('wp_ajax_postviews', 'tie_increment_views');
add_action('wp_ajax_nopriv_postviews', 'tie_increment_views');
function tie_increment_views() {
  global $wpdb;
  if(!empty($_GET['postviews_id']))
  {
    $post_id = intval($_GET['postviews_id']);
    if($post_id > 0 && defined('WP_CACHE') && WP_CACHE) {
      $count    = 0;
      $count_key  = 'tie_views';
      $count    = (int)get_post_meta($post_id, $count_key, true);
      $count++;

      update_post_meta($post_id, $count_key, (int)$count);
      echo $count;
    }
  }
  exit();
}

// Add it to a column in WP-Admin 
add_filter('manage_posts_columns', 'tie_posts_column_views');
add_action('manage_posts_custom_column', 'tie_posts_custom_column_views',5,2);
function tie_posts_column_views($defaults){
    $defaults['tie_post_views'] = __( 'Views','tie' );
    return $defaults;
}
function tie_posts_custom_column_views($column_name, $id){
  if( $column_name === 'tie_post_views' ){
        echo tie_views( '', get_the_ID() );
    }
}
?>

然后在single.php里面头部get_header() 函数上面添加函数:tie_setPostViews() 就可以了。这样每次浏览文章的时候就会自动执行tie_setPostViews() 函数来新增一次浏览次数的字段。

这种模式不能开启缓存,开启缓存了要用到ajax的方式。这个方式下次我们会讲到。

Check Also

input file字段选择图片之后怎么实现预览(还未上传到服务器的情况下)

$("#txt_image") …

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注