如题,找了很多博客文章,最终还是没有解决问题。侧边栏还是不能很好的显示浏览最多的文章;放弃了在侧边显示后,改在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插件;