首页CMSTypecho主题开发/二次开发常用代码功能整理备用

Typecho主题开发/二次开发常用代码功能整理备用

时间2024-05-23 07:16:04发布访客分类CMS浏览786
导读:目前我们在使用的博客CMS程序中,可能较多的网友会使用WordPress,毕竟提供的免费主题、插件以及文档是比较多的,主要是用户量确实比较多。其次国内的免费博客CMS中,ZBLOG和Typecho是小众用户群,不能说不行,只能说用户量相对比...

目前我们在使用的博客CMS程序中,可能较多的网友会使用WordPress,毕竟提供的免费主题、插件以及文档是比较多的,主要是用户量确实比较多。其次国内的免费博客CMS中,ZBLOG和Typecho是小众用户群,不能说不行,只能说用户量相对比较小。但是老蒋个人认为有用作个人博客日志的还是可以用的。

尤其是我们喜欢折腾程序的朋友在选择到免费主题之后可能需要对其进行二次开发,当然有些希望深入开发的朋友会去做一些特有的主题,自用或者分享。老蒋有些时候也需要用到客户中的网站进行二次开发功能或者仿站主题会有用到Typecho,这里我也顺手根据网上网友提供的一些代码整合进行整理。

这里的代码可能后续也会补充,是我们常用到Typecho主题开发和二次开发需要用到的一些代码功能。部分代码确实是使用的转载或者不知道出处的,这里感谢提供的网友。

1、文章缩略图调用

/** 输出文章缩略图 */ function showThumbnail($widget){
         // 当文章无图片时的默认缩略图    $rand = rand(1,5);
     // 随机 1-5 张缩略图    $random = $widget->
    widget('Widget_Options')->
    themeUrl . '/img/sj/' . $rand . '.jpg';
     // 随机缩略图路径   // $random = $widget->
    widget('Widget_Options')->
    themeUrl . '/img/mr.jpg';
     // 若只想要一张默认缩略图请删除本行开头的"//"    $attach = $widget->
    attachments(1)->
    attachment;
        $pattern = '/\img.*?src\=\"(.*?)\"[^>
    ]*>
    /i';
         if (preg_match_all($pattern, $widget->
content, $thumbUrl)) {
             echo $thumbUrl[1][0];
    }
     else     if ($attach->
isImage) {
          echo $attach->
    url;
     }
 else {
            echo $random;
    }
}
    

这里我们先默认1-5个默认图片,在文章没有图片的时候我们可以默认显示。

?php showThumbnail($this);
     ?>

合适的主题位置调用。

2、获取文章第一张图作为缩略图

function showThumbnail($widget) {
    $attach = $widget->
    attachments(1)->
    attachment;
    $pattern = '/\img.*?src\=\"(.*?)\"[^>
    ]*>
    /i';
    if (preg_match_all($pattern, $widget->
content, $thumbUrl)) {
    echo $thumbUrl[1][0];
}
     elseif ($attach->
isImage) {
    echo $attach->
    url;
}
 else {
    echo $random;
}
 }
    

调用方式:

img src="?php showThumbnail($this);
     ?>
    ">
    

3、相关文章调用

?php $this->
    related(5)->
    to($relatedPosts);
     ?>
        ul>
        ?php while ($relatedPosts->
    next()): ?>
        li>
    a href="?php $relatedPosts->
    permalink();
     ?>
    " title="?php $relatedPosts->
    title();
     ?>
    ">
    ?php $relatedPosts->
    title();
     ?>
    /a>
    /li>
        ?php endwhile;
     ?>
    /ul>

4、热门文章调用

function getHotComments($limit = 10){
        $db = Typecho_Db::get();
        $result = $db->
    fetchAll($db->
    select()->
    from('table.contents')        ->
    where('status = ?','publish')        ->
    where('type = ?', 'post')        ->
    where('created = unix_timestamp(now())', 'post') //添加这一句避免未达到时间的文章提前曝光        ->
    limit($limit)        ->
    order('commentsNum', Typecho_Db::SORT_DESC)    );
    if($result){
        foreach($result as $val){
                            $val = Typecho_Widget::widget('Widget_Abstract_Contents')->
    push($val);
                $post_title = htmlspecialchars($val['title']);
                $permalink = $val['permalink'];
                echo 'li>
    a href="'.$permalink.'" title="'.$post_title.'" target="_blank">
    '.$post_title.'/a>
    /li>
    ';
                }
    }
}
    

调用方式:

?php getHotComments('10');
    ?>
    

根据需要修改调用数量和样式。

5、侧栏热门标签调用

div class="widget">
    h3>
    ?php _e('热门标签');
     ?>
    /h3>
    ul class="cate">
    ?php $this->
    widget('Widget_Metas_Tag_Cloud', array('sort' =>
     'count', 'ignoreZeroCount' =>
     true, 'desc' =>
     true, 'limit' =>
     20))->
    to($tags);
     ?>
      ?php while($tags->
    next()): ?>
      li>
    a rel="tag" href="?php $tags->
    permalink();
     ?>
    ">
    ?php $tags->
    name();
     ?>
    /a>
    /li>
    ?php endwhile;
     ?>
    div class="clear">
    /div>
    /ul>
    /div>

6、文章统计代码实现

//统计调用实现 itbulu.comfunction get_post_view($archive){
        $cid    = $archive->
    cid;
        $db     = Typecho_Db::get();
        $prefix = $db->
    getPrefix();
        if (!array_key_exists('views', $db->
    fetchRow($db->
    select()->
from('table.contents')))) {
            $db->
    query('ALTER TABLE `' . $prefix . 'contents` ADD `views` INT(10) DEFAULT 0;
    ');
            echo 0;
            return;
    }
        $row = $db->
    fetchRow($db->
    select('views')->
    from('table.contents')->
    where('cid = ?', $cid));
        if ($archive->
is('single')) {
           $db->
    query($db->
    update('table.contents')->
    rows(array('views' =>
     (int) $row['views'] + 1))->
    where('cid = ?', $cid));
    }
        echo $row['views'];
}
    

调用:

?php get_post_view($this) ?>
    

需要的位置调用。

7、分页代码

?php $this->
    pageNav('上一页', '下一页', '5', '……');
     ?>
    //显示多个页码的?php $this->
    pageLink('下一页','next');
     ?>
    ?php $this->
    pageLink('上一页');
     ?>
    //只显示上一页下一页

这里我们在之前也有整理:ZBLOG PHP程序可能用到的分页标签调用方法

8、上一篇下一篇

上一篇: ?php $this->
    thePrev('%s','没有了');
     ?>
    下一篇: ?php $this->
    theNext('%s','没有了');
     ?>
    

9、搜索代码

form method="post">
    p>
    input type="text" name="s" class="text" autofocus />
    /p>
    p>
    button type="submit" class="submit">
    ?php _e('搜索');
     ?>
    /button>
    /p>
    /form>
    

10、最新文章调用

?php $this->
    widget('Widget_Contents_Post_Recent')->
    to($post);
     ?>
    ?php while($post->
    next()): ?>
    a href=”?php $post->
    permalink();
     ?>
    ” title=”?php $post->
    title();
     ?>
    ”>
    ?php $post->
    title(25, '…');
     ?>
    /a>
    ?php endwhile;
     ?>
    

以后有其他的老蒋再整理。

参考文献:

1、https://qqdie.com/archives/typecho-code.html

2、https://www.moewah.com/archives/63.html

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Typecho主题开发/二次开发常用代码功能整理备用
本文地址: https://pptw.com/jishu/666151.html
span 和 strong 等元素称为“行内元素” WordPress 标签关键字Tag自动分布和批量设置插件

游客 回复需填写必要信息