在网络上浏览的时候,发现有的网站上用的DUX主题首页有个热门文章模块看着不错,就想着给网站也添加上,于是百度了一下,在蝈蝈要安静的网站上找到了,说是模仿XIU主题的,于是我就自己从XIU主题里提取需要的代码和文件为网站添加上了,具体效果如下图:
后台设置图:
修改步骤如下:
- 新建一个名为
hui_recent_posts_most.php
的文件,把下面这段代码保存到文件中,或者直接到XIU主题的models
文件夹中直接提取出来,然后放入主题的models
文件夹下即可,为什么是models文件夹呢?这是因为主题在functions-theme.php
文件夹中添加了一个_moloader
的函数用于调取模板,这里为了方便我们直接使用该函数调取模板即可。<?php /* * recent post most * ==================================================== */ function hui_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' ){ $today = date("Y-m-d H:i:s"); $daysago = date( "Y-m-d H:i:s", strtotime($today) - ($days * 24 * 60 * 60) ); $result = $wpdb->get_results("SELECT comment_count, ID, post_title, post_date FROM $wpdb->posts WHERE post_date BETWEEN '$daysago' AND '$today' AND 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></p><span class="label label-'.$i.'">'.$i.'</span><a href="'.get_permalink($postid).'" title="'.$title.'">'.$title.'</a></li>'; $i++; } } } }else if( _hui('most_list_style')=='view' ){ global $post; $limit_date = current_time('timestamp') - ($days*86400); $limit_date = date("Y-m-d H:i:s",$limit_date); $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 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"); 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>'.hui_get_post_date( 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></p><span class="label label-'.$i.'">'.$i.'</span><a href="'.get_permalink($post->ID).'" title="'.$title.'">'.$title.'</a></li>'; $i++; } } else { $output = '<li>'.__('暂无文章!', 'haoui').'</li>'; } }else if( _hui('most_list_style')=='like' ){ global $post; $limit_date = current_time('timestamp') - ($days*86400); $limit_date = date("Y-m-d H:i:s",$limit_date); $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 `like` 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 = 'like' AND post_password = '' ORDER BY `like` DESC LIMIT $limit"); if($most_viewed) { $i = 1; foreach ($most_viewed as $post) { $title = get_the_title().get_the_subtitle(); $post_likes = intval($post->like); // $output .= '<li class="item-'.$i.'"><a target="_blank" href="'.get_permalink($postid).'">'._get_post_thumbnail(array()).'<h2>'.$post_title.'</h2><p>'.hui_get_post_date( get_the_time('Y-m-d H:i:s') ).'<span class="post-views">阅读('.$post_views.')</span></p></a></li>'; $output .= '<li><span class="label label-'.$i.'">'.$i.'</span><a href="'.get_permalink($post->ID).'" title="'.$title.'">'.$title.'</a></li>'; $i++; } } else { $output = '<li>'.__('暂无文章!', 'haoui').'</li>'; } } echo '<div class="most-comment-posts"> <div class="title"><h3>'._hui('most_list_title').'</h3></div> <ul>'.$output.'</ul> </div>'; }
- 修改
options.php
文件,在文件末尾return $options;
之前添加以下代码:$options[] = array( 'name' => __('热门排行功能', 'haoui'), 'type' => 'heading'); $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'), 'like' => __('按文章点赞数', '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', 'type' => 'text'); $options[] = array( 'name' => __('热门排行', 'haoui').$rrr.__('显示数量', 'haoui'), 'id' => 'most_list_number', 'std' => 5, 'class' => 'mini', 'type' => 'text');
- 修改
index.php
文件,添加下面的代码至你想要显示热门文章列表的位置即可。<!--热门文章排行--> <?php if( _hui('most_list_s') ){ _moloader('hui_recent_posts_most'); } ?>
- 添加以下样式到
main.css
© 版权声明
这些信息可能会帮助到你: 下载帮助 | 免责说明 | 进站必看 | 广告投放
版权声明:本文采用知识共享 署名4.0国际许可协议 BY-NC-SA 4.0 进行授权文章名称:《为DUX主题首页添加热门文章模块》
文章链接:https://www.jsksky.com/add-popular-article-module-for-wordpress-theme-dux-homepage.html
免责声明:根据《计算机软件保护条例》第十七条规定“为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。”您需知晓本站所有内容资源均来源于网络,仅供用户交流学习与研究使用,版权归属原版权方所有,版权争议与本站无关,用户本人下载后不能用作商业或非法用途,需在24个小时之内从您的电脑中彻底删除上述内容,否则后果均由用户承担责任;如果您访问和下载此文件,表示您同意只将此文件用于参考、学习而非其他用途,否则一切后果请您自行承担,如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
本站为个人博客非盈利性站点,所有软件信息均来自网络,所有资源仅供学习参考研究目的,并不贩卖软件,不存在任何商业目的及用途,部分软件收费或网站会员捐赠是您喜欢本站而产生的赞助支持行为,仅为维持服务器的开支与维护,全凭自愿无任何强求。
THE END
请登录后发表评论
注册
社交帐号登录