山滚云

WordPress根据文章浏览量进行排序教程

功能代码 458℃

我们想要实现最多人看的功能就必须对WordPress的代码风格及编写规范有一定了解,因此仅适合具备一定编程基础的人进行二次开发或者直接使用现成的代码,接下来为大家分享一个WordPress根据文章浏览量进行排序教程。

将下方代码添加进functions.php外加一个php文件即可实现:

//根据浏览量从多到少进行排序

function get_most_viewed_format($mode = ”, $limit = 10, $show_date = 0, $term_id = 0, $beforetitle= ‘(‘, $aftertitle = ‘)’, $beforedate= ‘(‘, $afterdate = ‘)’, $beforecount= ‘(‘, $aftercount = ‘)’) {

global $wpdb, $post;

$output = ”;

$mode = ($mode == ”) ? ‘post’ : $mode;

$type_sql = ($mode != ‘both’) ? “AND post_type=’$mode'” : ”;

$term_sql = (is_array($term_id)) ? “AND $wpdb->term_taxonomy.term_id IN (” . join(‘,’, $term_id) . ‘)’ : ($term_id != 0 ? “AND $wpdb->term_taxonomy.term_id = $term_id” : ”);

$term_sql.= $term_id ? ” AND $wpdb->term_taxonomy.taxonomy != ‘link_category'” : ”;

$inr_join = $term_id ? “INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)” : ”;

// database query

$most_viewed = $wpdb->get_results(“SELECT ID, post_date, post_title, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) $inr_join WHERE post_status = ‘publish’ AND post_password = ” $term_sql $type_sql AND meta_key = ‘views’ GROUP BY ID ORDER BY views DESC LIMIT $limit”);

if ($most_viewed) {

foreach ($most_viewed as $viewed) {

$post_ID = $viewed->ID;

$post_views = number_format($viewed->views);

$post_title = esc_attr($viewed->post_title);

$get_permalink = esc_attr(get_permalink($post_ID));

$output .= ‘<li><a href=”‘.get_permalink($post_ID).'”>’.$post_title.”;

if ($show_date) {

$posted = date(get_option(‘date_format’), strtotime($viewed->post_date));

$output .= “$beforedate $posted $afterdate”;

}

$output .= “$beforecount $post_views $aftercount</a></li>”;

}

} else {

$output = “<li>N/A</li>n”;

}

echo $output;

}

//在需要调用的地方插入下方代码

<?php get_most_viewed_format(); ?>

转载请注明:⎛蜗牛建站⎞ » WordPress根据文章浏览量进行排序教程

蜗牛建站是专业的wordpress网站建设服务团队,我们有丰富的wordpress网站建设经验,可以提供wordpress网站建设、网站改版、主题开发、wordpress外贸建站等服务。

城市分站:深圳建站公司北京网站建设上海网站建设广州网站建设深圳网站建设成都网站建设重庆网站建设武汉网站建设

业务范围:深圳建站公司,网站优化,虚拟主机    微信:ganshangwoniu    QQ:549233124    业务咨询热线:177-2050-9380

   Copyright © 2008-2024 蜗牛建站版权所有   

QQ在线咨询
wordpress主题开发公司微信号