不用插件在侧边栏显示浏览最多的文章| Rtrash|废纸篓

如题,找了很多博客文章,最终还是没有解决问题。侧边栏还是不能很好的显示浏览最多的文章;放弃了在侧边显示后,改在sidbar.php中加入显示代码吧。

起初想在侧边栏显示浏览最多的文章,主要好处在于能随意移动其显示的位置;侧边栏显示内容我几乎用的全是小工具来控制的;但浏览最多的文章既然是统计出最热的文章,那只好是放在侧栏{sg}位置了;这样也不用纠结在widget上了。

其实此方法非常简单的,内容如下:

进行到这里的时候一定要保证(functions.php) 中有2个函数:

function _get_permalink

function_exists(‘_get_most_viewed’)

如果没有这两个函数的必须加入functions.php中,下面是2个函数内容。

function _get_permalink($id = 0) {
global $index_filename;

$post = &get_post($id);

if ( empty($post->ID) ) return FALSE;

if ( !empty($index_filename) ) return $index_filename . “?p=” . $post->ID;

return “index.php?p=” . $post->ID;
}

if(!function_exists(‘_get_most_viewed’)) {
function _get_most_viewed($mode = , $limit = 10, $chars = 0, $display = true) {
global $wpdb, $post;
$where = ;
$temp = ;
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 $where AND post_status = ‘publish’ AND meta_key = ‘views’ AND post_password = ” ORDER? BY views DESC LIMIT $limit”);
if($most_viewed) {
$temp .= ‘<div class=”page”><ol id=”pages”>’;
if($chars > 0) {
foreach ($most_viewed as $post) {
$post_title = get_the_title();
$post_views = intval($post->views);
$post_views = number_format($post_views);
$temp .= “<li class=\”page_item page-item-2\”><a href=\”" . _get_permalink($post->ID) . “\”>”.snippet_chars($post_title, $chars).“</a> – $post_views “.__(‘views’, ‘wp-postviews’).“</li>”;
}
} else {
foreach ($most_viewed as $post) {
$post_title = get_the_title();
$post_views = intval($post->views);
$post_views = number_format($post_views);
$temp .= “<li class=\”page_item page-item-2\”><a href=\”" . _get_permalink($post->ID) . “\”>$post_title</a></li>”;
}
}
$temp .= ‘</ol></div>’;
} else {
$temp = __(‘No Posts’, ‘wap’).‘<br/>’;
}
if($display) {
echo $temp;
} else {
return $temp;
}
}
}

贴入代码保存后就可以在侧边栏显示了,前提是你安装了post view flus插件;

可能关联的文章:


?

评论暂缺

:twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O 8)
郑重声明:资讯 【不用插件在侧边栏显示浏览最多的文章| Rtrash|废纸篓】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——