`
yantaoliu2006
  • 浏览: 93757 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

wp热门文章调用方法

 
阅读更多

wordpress模板制作  过程中,经常会用到文章的调用,下面给大家介绍一下wordpress模板制作中的热评文章的调用方法。

1、在functions.php函数文件中添加如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 获得热评文章
function simple_get_most_viewed($posts_num=10, $days=300){
    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
           AND ($wpdb->posts.`post_status` = 'publish' OR $wpdb->posts.`post_status` = 'inherit')
           ORDER BY comment_count DESC LIMIT 0 , $posts_num "
;
    $posts = $wpdb->get_results($sql);
    $output = "";
    foreach ($posts as $post){
        $output .= "\n<li><a href= \"".get_permalink($post->ID)."\" rel=\"bookmark\" title=\"".$post->post_title." (".$post->comment_count."条评论)\" >". $post->post_title."</a></li>";
    }
    echo $output;
}
?>

2、在需要添加热评文章列表的地方添加如下代码:

1
<?php simple_get_most_viewed(); ?>

这个方法是通过函数调用了有评论的文章,按照评论的多少从大到小排序,以列表的形式展现出来。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics