首先,配置文件里面的functions.php,我的是这样的
/**
* 阅读统计
* 调用*/
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 = '
- ( 热度: ', $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']);
https://www.veimoz.com/$permalink = $result['permalink'];
echo " ";
}
} else {
echo "N/A \n";
}
}
这样就可以直接获取了, 可以获取到标题,链接,阅读统计
如果获取图片的话,因为图片是自定义字段,不能直接调出来,写法如下:
//热门访问量文章
function theMostViewed($limit = 3, $before = '
- ( 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']);
https://www.veimoz.com/$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 "";
}
} else {
echo "N/A \n";
}
}
前端调用代码:
!
发表评论