typecho热门文章按访问量排序的自定义扩展
首先,配置文件里面的functions.php,我的是这样的
/**
* 阅读统计
* 调用<?php get_post_view($this); ?>
*/
function Postviews($archive) {
$db = Typecho_Db::get();
$cid = $archive->cid;
if (!array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))) {
$db->query('ALTER TABLE `'.$db->getPrefix().'contents` ADD `views` INT(10) DEFAULT 0;');
}
$exist = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid))['views'];
if ($archive->is('single')) {
$cookie = Typecho_Cookie::get('contents_views');
$cookie = $cookie ? explode(',', $cookie) : array();
if (!in_array($cid, $cookie)) {
$db->query($db->update('table.contents')
->rows(array('views' => (int)$exist+1))
->where('cid = ?', $cid));
$exist = (int)$exist+1;
array_push($cookie, $cid);
$cookie = implode(',', $cookie);
Typecho_Cookie::set('contents_views', $cookie);
}
}
echo $exist == 0 ? '0' : $exist.' ';
}
获取文章的统计代码如下:
function theMostViewed($limit = 10, $before = '<br/> - ( 热度: ', $after = ' 度 ) ')
{
$db = Typecho_Db::get();
$options = Typecho_Widget::widget('Widget_Options');
$limit = is_numeric($limit) ? $limit : 5;
$posts = $db->fetchAll($db->select()->from('table.contents')
->where('type = ? AND status = ? AND password IS NULL', 'post', 'publish')
->order('views', Typecho_Db::SORT_DESC)
->limit($limit)
);
if ($posts) {
foreach ($posts as $post) {
$result = Typecho_Widget::widget('Widget_Abstract_Contents')->push($post);
$post_views = number_format($result['views']);
$post_title = htmlspecialchars($result['title']);
$permalink = $result['permalink'];
echo "<li><div class='widget-posts-text'><a class='widget-posts-title' href='$permalink' title='$post_title'>$post_title</a><div class='widget-posts-meta'><i>$post_views °c</i></div></div></li>";
}
} else {
echo "<li>N/A</li>\n";
}
}
这样就可以直接获取了, 可以获取到标题,链接,阅读统计
如果获取图片的话,因为图片是自定义字段,不能直接调出来,写法如下:
//热门访问量文章
function theMostViewed($limit = 3, $before = '<br/> - ( views: ', $after = ' times ) ')
{
$db = Typecho_Db::get();
$options = Typecho_Widget::widget('Widget_Options');
$limit = is_numeric($limit) ? $limit : 5;
$posts = $db->fetchAll($db->select()->from('table.contents')
->where('type = ? AND status = ? AND password IS NULL', 'post', 'publish')
->order('views', Typecho_Db::SORT_DESC)
->limit($limit)
);
if ($posts) {
foreach ($posts as $post) {
$result = Typecho_Widget::widget('Widget_Abstract_Contents')->push($post);
$post_views = number_format($result['views']);
$post_title = htmlspecialchars($result['title']);
$permalink = $result['permalink'];
$created = date('m-d', $result['created']);
$img = $db->fetchAll($db->select()->from('table.fields')->where('name = ? AND cid = ?','img',$result['cid']));
if(count($img) !=0){
//var_dump($img);
$img=$img['0']['str_value'];
if($img){}
else{
$img="/usr/themes/spimes/images/thumbs/other_thumbnail.png";
}
}
// var_dump($img);
// if($img == ""){
// $img = "wu";
// }
echo "<div class='py-2'><div class='list-item list-overlay-content'><div class='media media-2x1'><a class='media-content' href='$permalink' style='background-image:url(".$img.");'><span class='overlay'></span></a></div><div class='list-content'><div class='list-body'><a href='$permalink' class='list-title h-2x'>$post_title</a></div><div class='list-footer'><div class='text-muted text-xs'>$post_views 阅读 ,<time class='d-inline-block'>$created</time></div></div></div></div></div>";
}
} else {
echo "<li>N/A</li>\n";
}
}
前端调用代码:
<?php theMostViewed(); ?>
文章由官网发布,如若转载,请注明出处:https://www.dpaoz.com/75
0 评论
2.3k
发表评论

热门文章
自媒体博客Spimes主题11w 阅读
Spimes主题专为博客、自媒体、资讯类的网站设计....
Spzac个人资讯下载类主题6.7w 阅读
用于作品展示、资源下载,行业垂直性网站、个人博客,....
vCard主题个人简历主题6.5w 阅读
一款个人简历主题,可以简单搭建一下,具体也比较简单....
Splity博客双栏主题4.9w 阅读
仿制主题,Typecho博客主题,昼夜双版设计,可....
热评文章
自媒体博客Spimes主题337 评论
Splity博客双栏主题171 评论
Spzac个人资讯下载类主题82 评论
Spzhi知识付费社区主题34 评论
Splinx博客图片主题33 评论
vCard主题个人简历主题27 评论
Pure轻简主题23 评论
Spkan视频影视类模板21 评论
axiomxs
昨天 22:26
作者老大哥,求说说页面啊