WordPress根据30天/90天/1年 阅读点击数调用热门文章

一般我们玩个人博客的时候,无论是购买的主题,还是免费主题,都是比较就简单的内容列表功能,侧栏调用最新文章或者使用模块调用。当然可以用插件实现调用随机文章、热门评论文章等等。但是我们在做企业网站的时候,由于特殊性不可能都实现自动化。有些时候都在一些基础的模板中修改的,然后加一些特有的功能。

有些企业网站中为了凸显内容不至于空空的,需要在侧栏空的位置调用一些内容。比如目前手上的一个企业网站 客户准备调用一些点击较高的文章。这里就不用插件,直接找到代码来解决。

第一、代码部分

//根据时间调用点击浏览人数多的文章
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;
} }

两处标注颜色的部分,分别是10篇文章和30天内的浏览人多的文章,我们可以根据实际修改。代码添加到当前主题Functions.php文件中。

第二、调出部分

<?php
if (function_exists('get_mostViewedPostList') & function_exists('mostViewedPostList')){
echo '<ul>';
//10为调用的篇数,根据自己实际情况修改
mostViewedPostList('post',10);
echo '</ul>';
}
?>

这样将调用内容添加到模板中需要显示的位置。

本文出处:老蒋部落 » WordPress根据30天/90天/1年 阅读点击数调用热门文章 | 欢迎分享( 公众号:老蒋朋友圈 )

公众号 「老蒋朋友圈」获取站长新知 / 加QQ群 【1012423279】获取商家优惠推送