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.veimoz.com/75
0 评论
3.5k

发表评论

!