摘要:实现一个wordpress文章阅读排名的功能,可以通过以下代码实现,但是需要一个前提就是你的当前wordpress主题需…
实现一个wordpress文章阅读排名的功能,可以通过以下代码实现,但是需要一个前提就是你的当前wordpress主题需要集成或是安装了WP-PostViews文章浏览数据统计插件,才能够综合应用到我们接下来的代码中,实现我们的阅读量排行功能,下面大挖将以30天内的点击文章浏览量排名为基础分享给大家以下的操作步骤,大家可以举一反三,作出周排名、月排名甚至24小时排名。
一、找到当前主题中的functions.php文件,然后将以睛代码复制进该文件中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
//本文代码为30天内点击排行榜
//请将此代码放在functions.php文件的<?php和?>之间的位置
//编码时请选择专业的编码工具
function mostViewedPostList($mode = ”, $limit = 10, $chars = 0, $display = true) {
global $wpdb, $post;
$views_options = get_option(‘views_options’);
$where = ”; $temp = ”;
$output = ”; if(!emptyempty($mode) && $mode != ‘both’) {
$where = “post_type = ‘$mode‘”;
} else { $where = ‘1=1′; } $mostViewedPostList = $wpdb->get_results(“SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date > ‘” . date(‘Y-m-d’, strtotime(‘-30 days’)) . “‘ AND $where AND post_status = ‘publish’ AND meta_key = ‘views’ AND post_password = ” ORDER BY views DESC LIMIT $limit”);
if($mostViewedPostList) {
foreach ($mostViewedPostList as $post) {
$post_views = intval($post->views);
$post_title = get_the_title(); if($chars > 0) {
$post_title = snippet_text($post_title, $chars);
} $post_excerpt = views_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password, $chars);
$post_content = get_the_content();
$temp = stripslashes($views_options[‘mostViewedPostList_template’]);
$temp = str_replace(“%VIEW_COUNT%”, number_format_i18n($post_views), $temp);
$temp = str_replace(“%POST_TITLE%”, $post_title, $temp);
$temp = str_replace(“%POST_EXCERPT%”, $post_excerpt, $temp);
$temp = str_replace(“%POST_CONTENT%”, $post_content, $temp);
$temp = str_replace(“%POST_URL%”, get_permalink(), $temp);
$output .= $temp;
} } else { $output = ‘<li>N/A</li>’.“\n”;
} if($display) {
echo $output; } else { return $output;
} }
|
找到我们需要显示排名的位置或页面,将下面代码复制到页面内。
1
2
3
4
5
6
7
8
|
<?php
if (function_exists(‘get_mostViewedPostList’) & function_exists(‘mostViewedPostList’)){
echo ‘<ul>‘;
//10为调用的篇数,根据自己实际情况修改
mostViewedPostList(‘post’,10);
echo ‘</ul>’;
}
?>
|
来源:http://www.wazhuti.com/2076.html