类别导航:
通知:目前网站正常哦!
你的位置:首页 >> 编程技术 >> wordpress主题:非插件post_view

wordpress主题:非插件post_view

发表日期:2011-10-01 作者:DH分类:编程技术 标签: wordpress
索引[隐藏]

1 序言

首先祝大家国庆快乐! 最近准备抛弃willian大师的my_visitor,因为占用数据库实在是太大,但是其很好用的三个功能: 1. 简易post_view 2. 定期备份 3. 登录失败提醒 还是很好用的,这里我自己参考他的代码,写了简易的post_view,用于统计,代码很少,希望能给大家帮助

2 方法

2.2 dh_post_views.php

将下面的代码保存为dh_post_views.php
ID;
	  if($post_ID) 
	  {
		  $post_views = (int)get_post_meta($post_ID, 'views', true);
		  if(!update_post_meta($post_ID, 'views', ($post_views+1))) 
		  {
			add_post_meta($post_ID, 'views', 1, true);
		  }
	  }
	}
}
add_action('wp_head', 'record_visitors');  

/// 函数名称:post_views 
/// 函数作用:取得文章的阅读次数
function post_views($before = '(点击 ', $after = ' 次)', $echo = 1)
{
  global $post;
  $post_ID = $post->ID;
  $views = (int)get_post_meta($post_ID, 'views', true);
  if ($echo) echo $before, number_format($views), $after;
  else return $views;
}

/// get_most_viewed_format 
/// 函数作用:取得阅读最多的文章
function get_most_viewed_format($mode = '', $limit = 10, $show_date = 0, $term_id = 0, $beforetitle= '(', $aftertitle = ')', $beforedate= '(', $afterdate = ')', $beforecount= '(', $aftercount = ')') {
  global $wpdb, $post;
  $output = '';
  $mode = ($mode == '') ? 'post' : $mode;
  $type_sql = ($mode != 'both') ? "AND post_type='$mode'" : '';
  $term_sql = (is_array($term_id)) ? "AND $wpdb->term_taxonomy.term_id IN (" . join(',', $term_id) . ')' : ($term_id != 0 ? "AND $wpdb->term_taxonomy.term_id = $term_id" : '');
  $term_sql.= $term_id ? " AND $wpdb->term_taxonomy.taxonomy != 'link_category'" : '';
  $inr_join = $term_id ? "INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)" : '';

  // database query
  $most_viewed = $wpdb->get_results("SELECT ID, post_date, post_title, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) $inr_join WHERE post_status = 'publish' AND post_password = '' $term_sql $type_sql AND meta_key = 'views' GROUP BY ID ORDER BY views DESC LIMIT $limit");
  if ($most_viewed) {
   foreach ($most_viewed as $viewed) {
    $post_ID    = $viewed->ID;
    $post_views = number_format($viewed->views);
    $post_title = esc_attr($viewed->post_title);
    $get_permalink = esc_attr(get_permalink($post_ID));
    $output .= "
  • $beforetitle$post_title$aftertitle"; if ($show_date) { $posted = date(get_option('date_format'), strtotime($viewed->post_date)); $output .= "$beforedate $posted $afterdate"; } $output .= "$beforecount $post_views $aftercount
  • "; } } else { $output = "
  • N/A
  • \n"; } echo $output; } function get_totalviews($echo = 1) { global $wpdb; $total_views = $wpdb->get_var("SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = 'views'"); if ($echo) echo $total_views; else return $total_views; } ?>

    2.2 引用位置

    在functions.php中引用
    include("dh_post_views.php");
    

    3 说明

    3.1 需要在要使用的地方使用函数

    阅读:
    

    3.2 这里还提供了两个函数

    (1)get_most_viewed_format 得到最多阅读的文章,列出5个。 (2)get_totalviews 得到评论的总数

    除非注明,文章均为灯火部落原创,转载请注明出处:wordpress主题:非插件post_view-灯火部落
    相关文章:
  • wordpress主题:添加文章目录
  • wordpress主题:博客主题新功能
  • wordpress安全:处理垃圾评论(续)-前台管理评论
  • wordpress ajax之六:评论表情动态加载
  • wordpress数据库:定期备份邮寄数据库
  • wordpress ajax之五:最近评论ajax分页
  • ∧回到顶部∧