wordpress热门点击文章和热门评论文章代码

本文将介绍关于wordpress本月热门点击文章代码,wordpress本年热门文章函数,wordpress本月热门评论文章代码,wordpress本年热门评论代码函数,你也许找了很久了吧。希望对你有用哦。

下面的代码是来自于彼岸世界的主题里面提取的函数,不过有一个缺陷,就是会出现自动保存和已经删除了的文章,所以如果要使用的话,还要修改一下哦。不懂的就不要折腾了。

[cce_php]
//本月热门文章
function get_timespan_month($mode = '', $limit = 8, $days = 30, $display = true) {
global $wpdb, $post;
$limit_date = current_time('timestamp') - ($days*86400);
$limit_date = date("Y-m-d H:i:s",$limit_date);
$where = '';
$str = '';
if(!empty($mode) && $mode != 'both') {
$where = "post_type = '$mode'";
} else {
$where = '1=1';
}
$most_viewed = $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 < '".current_time('mysql')."' AND post_date > '".$limit_date."' AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER  BY views DESC LIMIT $limit");
$str='<ul>';
if($most_viewed) {
$output = $pre_HTML;
$i=1;
foreach ($most_viewed as $post) {
$post_title = get_the_title();
$post_views = intval($post->views);
$post_views = number_format($post_views);
$str .= "<li><span class="tabNum">$i</span><a href="".get_permalink()."">$post_title</a> ".__('', 'wp-postviews')."</li>";
$i++;
}
}
else {
$str = '<ul><li>'.__('N/A', 'wp-postviews').'</li></ul>'."n";
}$str.='</ul>';
if($display) {
echo $str;
} else {
return $str;
}
}
//本月热门文章结束
//本年热门文章
function get_timespan_year($mode = '', $limit = 8, $days = 365, $display = true) {
global $wpdb, $post;
$limit_date = current_time('timestamp') - ($days*86400);
$limit_date = date("Y-m-d H:i:s",$limit_date);
$where = '';
$str = '';
if(!empty($mode) && $mode != 'both') {
$where = "post_type = '$mode'";
} else {
$where = '1=1';
}
$most_viewed = $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 < '".current_time('mysql')."' AND post_date > '".$limit_date."' AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER  BY views DESC LIMIT $limit");
$str='<ul>';
if($most_viewed) {
$output = $pre_HTML;
$i=1;
foreach ($most_viewed as $post) {
$post_title = get_the_title();
$post_views = intval($post->views);
$post_views = number_format($post_views);
$str .= "<li><span class="tabNum">$i</span><a href="".get_permalink()."">$post_title</a> ".__('', 'wp-postviews')."</li>";
$i++;
}
}
else {
$str = '<ul><li>'.__('N/A', 'wp-postviews').'</li></ul>'."n";
}$str.='</ul>';
if($display) {
echo $str;
} else {
return $str;
}
}
//本年热门文章结束
// 获取月度热评
function get_most_viewed_month($posts_num=8, $days=30){
    global $wpdb;

    $sql = "SELECT `ID` , `post_title` , `comment_count` FROM $wpdb->posts
            WHERE `post_type` = 'post' AND TO_DAYS( now( ) ) - TO_DAYS( `post_date` ) < $days
            ORDER BY `comment_count` DESC LIMIT 0 , $posts_num ";

    $posts = $wpdb->get_results($sql);

	$output = $pre_HTML;
$i=1;

    foreach ($posts as $post){
        $output .= "n<ul><li><span class="tabNum">$i</span>

		<a href= "".get_permalink($post->ID)."" rel="bookmark" title="" >".$post->post_title."</a></li></ul>";
    $i++;}

    echo $output;
}
// 获取年度热评
function get_most_viewed_year($posts_num=8, $days=365){
    global $wpdb;

    $sql = "SELECT `ID` , `post_title` , `comment_count` FROM $wpdb->posts
            WHERE `post_type` = 'post' AND TO_DAYS( now( ) ) - TO_DAYS( `post_date` ) < $days
            ORDER BY `comment_count` DESC LIMIT 0 , $posts_num ";

    $posts = $wpdb->get_results($sql);

	$output = $pre_HTML;
$i=1;

    foreach ($posts as $post){
        $output .= "n<ul><li><span class="tabNum">$i</span>

		<a href= "".get_permalink($post->ID)."" rel="bookmark" title="" >".$post->post_title."</a></li></ul>";
    $i++;}

    echo $output;
}
[/cce_php]

声明: 未经本站许可,谢绝转载!

1 个主题帖 其中:热心观众:0 个, 管理员:0 个

  1. 谢谢分享,正好要找这个代码。以后经常到博主的博客逛逛

    2012-06-25 13:13

抱歉,评论被关闭