WordPress大前端DUX主题首页添加热门排行功能

具体效果可以看本站首页

后台开关代码部署

找到主题目录的options.php在最下面添加以下代码

/**
     热门排行自定义后台设置
     www.vlwx.com
     */
	$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'),
		'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',
		'type' => 'text');

	$options[] = array(
		'name' => __('首页热门排行', 'haoui').$rrr.__('显示数量', 'haoui'),
		'id' => 'most_list_number',
		'std' => 5,
		'class' => 'mini',
		'type' => 'text');

首页代码部署

找到主题根目录的index.php文件搜索ads_index_01,在上面添加

功能代码部署

在主题的modules目录新建一个文件,文件名为mo_recent_posts_most.php,文件内容为以下代码

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 = '
  • '.__('暂无最多评论文章!', 'haoui').'
  • '; } else { $i = 1; foreach ($result as $C_topten) { $postid = $C_topten->ID; $title = $C_topten->post_title.get_the_subtitle(); $commentcount = $C_topten->comment_count; if ($commentcount != 0) { $output .= '
  •  '.__('评论', 'haoui').' ('.$commentcount.')

    '.$i.''.$title.'
  • '; $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 '".$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(); $commentcount = $post->comment_count; $post_views = intval($post->views); $output .= '
  •  '.__('阅读', 'haoui').' ('.$post_views.')

    '.$i.'ID).'" title="'.$title.'">'.$title.'
  • '; $i++; } } else { $output = '
  • '.__('暂无最多阅读文章!', 'haoui').'
  • '; } } echo '

    '._hui('most_list_title').'

      '.$output.'
    '; }

    添加css样式代码

    .mpc-bg {background:#FFF;padding:1px 20px;margin:10px 0px 15px 0px;}
    .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:#999;}
    .most-comment-posts ul{margin: 0 0 20px;padding: 20px 0 0;list-style: none;overflow: hidden;}
    .most-comment-posts li{font-size: 13px;white-space: nowrap; overflow: hidden; clear: both;text-overflow: ellipsis;}
    .most-comment-posts li > a span{color: #FF5E52;}
    .most-comment-posts p{float: right;font-size: 12px;color: #999;}
    .most-comment-posts .label{margin-right: 8px;padding: 2px 7px;top: -1px;}
    .label-1{background-color: #ff7878;}
    .label-2{background-color: #64CA38;}
    .label-3{background-color: #52BAF5;}
    .most-comment-posts .item-1 a {color:#ff7878}
    .most-comment-posts .item-2 a {color:#64CA38}
    .most-comment-posts .item-3 a {color:#52BAF5}
    .most-comment-posts a {color: #777;}
    .text-muted{color:#999}
    .most-comment-posts .item-1 a:hover,.most-comment-posts .item-2 a:hover,.most-comment-posts .item-3 a:hover {color:#FF5E52}

    给TA打赏
    共{{data.count}}人
    人已打赏
    WordPress

    wordpress无插件纯代码实现注册验证拒绝恶意注册

    2019-1-27 3:17:08

    WordPress

    WordPress如何在文章和评论中插入代码

    2019-4-19 23:49:29

    0 条回复 A文章作者 M管理员
      暂无讨论,说说你的看法吧
    个人中心
    有新私信 私信列表
    搜索