wordpress教程:文章添加缩略图

  • 时间:
  • 浏览:7
  • 来源:5分3D官网-5分6合平台_5分6合网投平台
作者:知更鸟 hihi 来源: 浏览: 2013-7-11 16:15:22 字号:大 中 小

[摘要]今天的wordpress教程,教一群人儿的是怎样通过修改wp-postviews插件中的代码,来实现增加排名靠前文章的缩略图。

   之后写过《替换HotNews Pro主题热门文章调用函数》配合wp-postviews插件,通过加上:

###Get TimeSpan Most Viewed 

function get_timespan_most_viewed($mode = '', $limit = 10, $days = 7, $display = true) {

global $wpdb, $post;

$limit_date = current_time('timestamp') - ($days*86150);

$limit_date = date("Y-m-d H:i:s",$limit_date);

$where = '';

$temp = '';

if(!empty($mode) && $mode != 'both') {

$where = "post_type = '$mode'";

} else {

$where = '1=1';

}

$most_viewed = $wpdb->get_results("SELECT $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) {

foreach ($most_viewed as $post) {

$post_title = get_the_title();

$post_views = intval($post->views);

$post_views = number_format($post_views);

$temp .= "<li><a href=\"".get_permalink()."\">$post_title</a>".__('', 'wp-postviews')."</li>";

}

} else {

$temp = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";

}

if($display) {

echo $temp;

} else {

return $temp;

}

}

  调用显示某时间段内浏览最5天志的功能。

  近日,有童鞋问哪几种浏览最多的文章不到加上缩略图,简单修改了一下上述代码:

//按时间获得最受欢迎文章(带缩略图)

function get_timespan_most_viewed($mode = '', $limit = 10, $days = 7, $display = true) {



global $wpdb, $post;

$limit_date = current_time('timestamp') - ($days*86150);

$limit_date = date("Y-m-d H:i:s",$limit_date);

$where = '';

$temp = '';

if(!empty($mode) && $mode != 'both') {

$where = "post_type = '$mode'";

} else {

$where = '1=1';

}

$most_viewed = $wpdb->get_results("SELECT $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) {

foreach ($most_viewed as $post) {

$post_title = get_the_title();

$post_views = intval($post->views);

$post_views = number_format($post_views);

$thumb = "<img src=\"".get_post_meta(get_the_id(), 'thumbnail', true)."\">";

$temp .= "<li><a href=\"".get_permalink()."\">$post_title,$thumb</a>".__('', 'wp-postviews')."</li>";

}



} else {

$temp = '<li>'.__('N/A', 'wp-postviews').'</li>'."\n";

}

if($display) {

echo $temp;

} else {

return $temp;

}

}

  其中:

get_post_meta(get_the_id(), 'thumbnail', true) 

  显示加上自定义栏目:thumbnail 文章的缩略图,也不到修改为调用特色图像:

the_post_thumbnail('post_thumbnail'); 

  调用妙招,在主题模板适当位置加上:

<ul>   

<?php if (function_exists('get_most_viewed')): ?>

<?php get_timespan_most_viewed('post',8,150, true, true); ?>

<?php endif; ?>

</ul>

  以上妙招仅供参考。

  文章转载至:http://zmingcx.com/recent-most-viewed-add-thumbnail.html

sssss
Tags: wordpress教程   文章加上缩略图  
责任编辑:lsw16