查看目录
用wordpress建站的一个好处就是可以有很多的主题模板可以选用,而且如果你觉得自己的主题有不尽如人意的地方,还可以进行DIY修改,比如楚狂人看到有的网站首页有热门排行模块,看上去不错,但是现在用的主题没有这个功能,怎么办?自己动手给主题添加一个热门排行功能就好。
要给网站添加热门排行功能,我们需要进行几个位置的修改:1、创建php文件,2、前台DIV代码,3、后台选项功能,4、CSS美化代码
添加热门排行文件
在主题的modules文件夹下面创建一个热门排行页面mo_recent_posts_most.php:
<?php /* * recent post most * ==================================================== */ function mo_recent_posts_most() { global $wpdb; // $days=400; $days = _hui('most_list_date'); $limit = _hui('most_list_number'); $output = ''; if( !_hui('most_list_style') || _hui('most_list_style')=='comment' ){ $datatime = ''; if($days) { $today = date("Y-m-d H:i:s"); $daysago = date( "Y-m-d H:i:s", strtotime($today) - ($days * 24 * 60 * 60) ); $datatime = "post_date BETWEEN '".$daysago."' AND '".$today."' AND"; } $result = $wpdb->get_results("SELECT comment_count, ID, post_title, post_date FROM $wpdb->posts WHERE $datatime post_status='publish' AND post_type='post' ORDER BY comment_count DESC LIMIT 0 , $limit"); if(empty($result)) { $output = '<li>'.__('暂无文章!', 'haoui').__('近期有评论的文章才会显示在这里,你也可以在主题设置中选择按阅读数排行。', 'haoui').'</li>'; } else { $i = 1; foreach ($result as $topten) { $postid = $topten->ID; $title = $topten->post_title.get_the_subtitle(); $commentcount = $topten->comment_count; if ($commentcount != 0) { $output .= '<li><p class="text-muted"><span class="post-comments">'.__('评论', 'haoui').' ('.$commentcount.')</span>'.hui_get_post_like($pid=$postid).'</p><span class="label label-'.$i.'">'.$i.'</span><a '._post_target_blank().' href="'.get_permalink($postid).'" title="'.$title.'">'.$title.'</a></li>'; $i++; } } } }else if( _hui('most_list_style')=='view' ){ global $post; $datatime = ''; if($days){ $limit_date = current_time('timestamp') - ($days*86400); $limit_date = date("Y-m-d H:i:s",$limit_date); $datatime = "post_date < '".current_time('mysql')."' AND post_date > '".$limit_date."' AND"; } $where = ''; $mode = 'post'; 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 $datatime $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit"); if($most_viewed) { $i = 1; foreach ($most_viewed as $post) { $title = get_the_title().get_the_subtitle(); $post_views = intval($post->views); //$output .= '<li class="item-'.$i.'"><a target="_blank" href="'.get_permalink($postid).'">'._get_post_thumbnail(array()).'<h2>'.$post_title.'</h2><p>'.get_the_time('Y-m-d H:i:s').'<span class="post-views">阅读('.$post_views.')</span></p></a></li>'; $output .= '<li><p class="text-muted"><span class="post-comments">'.__('阅读', 'haoui').' ('.$post_views.')</span>'.hui_get_post_like($post->ID).'</p><span class="label label-'.$i.'">'.$i.'</span><a '._post_target_blank().' href="'.get_permalink($post->ID).'" title="'.$title.'">'.$title.'</a></li>'; $i++; } } else { $output = '<li>'.__('暂无文章!', 'haoui').'</li>'; } } echo '<div class="most-comment-posts"> <h3 class="title"><strong>'._hui('most_list_title').'</strong></h3> <ul>'.$output.'</ul> </div>'; }
前台添加热门排行模块
DIV代码添加到首页适当位置:
if( $paged==1 && _hui('most_list_s') ) _moloader('mo_recent_posts_most');
后台添加热门排行选项:
options:
$options[] = array( 'name' => __('首页热门排行', 'haoui'), 'id' => 'most_list_s', 'std' => true, 'desc' => __('开启,在首页增加一个热门文章板块', 'haoui'), 'type' => 'checkbox'); $options[] = array( 'name' => __('热门排行', 'haoui').$rrr.__('模式', 'haoui').'(v5.3*)', 'id' => 'most_list_style', 'std' => "comment", 'type' => "radio", 'options' => array( 'comment' => __('按文章评论数', 'haoui'), 'view' => __('按文章阅读数', 'haoui'), )); $options[] = array( 'name' => __('热门排行', 'haoui').$rrr.__('标题', 'haoui'), 'id' => 'most_list_title', 'std' => __('一周热门排行', 'haoui'), 'type' => 'text'); $options[] = array( 'name' => __('热门排行', 'haoui').$rrr.__('最近多少天内的文章', 'haoui'), 'id' => 'most_list_date', 'std' => 7, 'class' => 'mini', 'desc' => __('留空表示不限制时间', 'haoui'), 'type' => 'text'); $options[] = array( 'name' => __('热门排行', 'haoui').$rrr.__('显示数量', 'haoui'), 'id' => 'most_list_number', 'std' => 5, 'class' => 'mini', 'type' => 'text');
热门排行CSS格式
添加到主题CSS文件中:
/*热门排行-----------chukuangren.com*/ .most-comment-posts{padding: 15px 15px 10px 15px;background-color:#fff;margin-bottom:15px;border: 1px solid #eee;} .most-comment-posts .title{background-color: #fff;border-bottom: none;border-bottom:1px solid #eee;padding-left:5px;} .most-comment-posts ul{padding: 15px 0 0 0;} .most-comment-posts li{overflow: hidden;clear: both;} .most-comment-posts li > a span{color: #FF5E52;} .most-comment-posts p{float: right;font-size: 12px;line-height:20px} .most-comment-posts .label{margin-right: 8px;padding: 2px 7px;top: -1px;} .label{position:relative;display:inline-block;padding:5px 7px;font-size:12px;line-height:14px;color:#ffffff;vertical-align:baseline;white-space:nowrap;background-color:#999999} .label-1{background-color: #FD8C84;} .label-2{background-color: #7FD75A;} .label-3{background-color: #60C4FD;} .most-comment-posts .like-btn{min-width: 50px;text-align: right;display: inline-block;margin-left: 5px;} .most-comment-posts .like-btn i{margin-right: 5px;top: 2px;width: 13px;} .most-comment-posts .title{position: relative;margin: 0;line-height: 30px;font-size: 18px;font-weight:700} .most-comment-posts .title strong{display: inline-block;position: relative;/* bottom: -2px; */}
通过以上四个步骤的修改,我们就给自己的wordpress网站主题添加了热门排行功能模块,效果如下:

热门排行效果
这个功能既可以用作自己网站前端美化,也可以添加到开发的主题模块中,造福更多的个人站长。