首先,配置文件里面的functions.php,我的是这样的
- /
- *阅读统计
- *调用<?phpget_post_view($this); ?>
- */
- functionPostviews($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)DEFAULT0;');
- }
- $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.' ';
- }
获取文章的统计代码如下:
- functiontheMostViewed($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 = ?ANDstatus = ?ANDpasswordISNULL', '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";
- }
}
这样就可以直接获取了, 可以获取到标题,链接,阅读统计
如果获取图片的话,因为图片是自定义字段,不能直接调出来,写法如下:
- //热门访问量文章
- functiontheMostViewed($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 = ?ANDstatus = ?ANDpasswordISNULL', '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 = ?ANDcid = ?','img',$result['cid']));
- if(count($img) !=0){
- //();
- $img=$img['0']['str_value'];
- if($img){}
- else{
- $img="/usr/themes/spimes/images/thumbs/other_thumbnail.png";
- }
- }
- //();
- //(){
- //=;
- // }
- echo "<div class='py-2'><div class='list-itemlist-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://typecho.firshare.cn/archives/1233.html
免责声明:文章内容不代表本站立场,本站不对其内容的真实性、完整性、准确性给予任何担保、暗示和承诺,仅供读者参考,文章版权归原作者所有。避免网络欺诈,本站不倡导任何交易行为。如您私自与本站转载自公开互联网中的资讯内容中提及到的个人或平台产生交易,则需自行承担后果。本站在注明来源的前提下推荐原文至此,仅作为优良公众、公开信息分享阅读,不进行商业发布、发表及从事营利性活动。如本文内容影响到您的合法权益(内容、图片等),请及时联系本站,我们会及时删除处理。