前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于typecho的一些样式代码

关于typecho的一些样式代码

作者头像
用户7146828
发布2021-08-09 14:54:40
1.2K0
发布2021-08-09 14:54:40
举报
文章被收录于专栏:旧云博客旧云博客

循环页面、分类、标签

代码语言:javascript
复制
<!--循环显示页面-->
        <?php $this->widget('Widget_Contents_Page_List')->to($pages); ?>
        <?php while($pages->next()): ?>

        <span class="nav-item<?php if($this->is('page', $pages->slug)): ?> nav-item-current<?php endif; ?>">
        <a href="<?php $pages->permalink(); ?>" title="<?php $pages->title(); ?>">
        <span><?php $pages->title(); ?></span>
        </a>
        </span>
        <?php endwhile; ?>
        <!--结束显示页面-->

        <!--循环所有分类-->
        <?php $this->widget('Widget_Metas_Category_List')->to($category); ?>
        <?php while ($category->next()): ?>

        <span class="nav-item<?php if($this->is('category', $category->slug)): ?> nav-item-current<?php endif; ?>">
        <a href="<?php $category->permalink(); ?>" title="<?php $category->name(); ?>">
        <span><?php $category->name(); ?></span>
        </a>
        </span>
        <?php endwhile; ?>
        <!--结束显示分类-->

        <!--循环显示标签 其中limit的5为显示数量-->
        <?php $this->widget('Widget_Metas_Tag_Cloud', array('sort' => 'count', 'ignoreZeroCount' => true, 'desc' => true, 'limit' => 5))->to($tags); ?>   
        <?php while($tags->next()): ?> 

        <span class="nav-item<?php if($this->is('tag', $tags->slug)): ?> nav-item-current<?php endif; ?>">
        <a href="<?php $tags->permalink(); ?>" title="<?php $tags->name(); ?>">
        <span><?php $tags->name(); ?></span>
        </a>
        </span>
        <?php endwhile; ?>
        <!--结束显示标签-->

标题栏

代码语言:javascript
复制
<title><?php if ($this->_currentPage > 1) echo '第 ' . $this->_currentPage . ' 页 - '; ?><?php $this->archiveTitle(array(
                'category'  =>  _t('分类 %s 下的文章'),
                'search'    =>  _t('包含关键字 %s 的文章'),
                'tag'       =>  _t('标签 %s 下的文章'),
                'author'    =>  _t('%s 发布的文章')
            ), '', ' - '); ?><?php $this->options->title(); ?><?php if ($this->is('index')): ?> - <?php $this->options->description(); ?><?php endif; ?></title>

首页置顶文章

<?php

代码语言:javascript
复制
$sticky = '3'; //置顶的文章cid,按照排序输入, 请以半角逗号或空格分隔
if($sticky && $this->is('index') || $this->is('front')){
    $sticky_cids = explode(',', strtr($sticky, ' ', ','));//分割文本 
    $sticky_html = "<span style='color:red'>[置顶] </span>"; //置顶标题的 html
    $db = Typecho_Db::get();
    $pageSize = $this->options->pageSize;
    $select1 = $this->select()->where('type = ?', 'post');
    $select2 = $this->select()->where('type = ? && status = ? && created < ?', 'post','publish',time());
    //清空原有文章的列队
    $this->row = [];
    $this->stack = [];
    $this->length = 0;
    $order = '';
    foreach($sticky_cids as $i => $cid) {
        if($i == 0) $select1->where('cid = ?', $cid);
        else $select1->orWhere('cid = ?', $cid);
        $order .= " when $cid then $i";
        $select2->where('table.contents.cid != ?', $cid); //避免重复
    }
    if ($order) $select1->order(null,"(case cid$order end)"); //置顶文章的顺序 按 $sticky 中 文章ID顺序
    if ($this->_currentPage == 1) foreach($db->fetchAll($select1) as $sticky_post){ //首页第一页才显示
        $sticky_post['sticky'] = $sticky_html;
        $this->push($sticky_post); //压入列队
    }
$uid = $this->user->uid; //登录时,显示用户各自的私密文章
    if($uid) $select2->orWhere('authorId = ? && status = ?',$uid,'private');
    $sticky_posts = $db->fetchAll($select2->order('table.contents.created', Typecho_Db::SORT_DESC)->page($this->_currentPage, $this->parameter->pageSize));
    foreach($sticky_posts as $sticky_post) $this->push($sticky_post); //压入列队
    $this->setTotal($this->getTotal()-count($sticky_cids)); //置顶文章不计算在所有文章内
}
?>    

博客信息

<?php if ($this->options->sidebarBlock && in_array('showSiteStatistics', $this->options->sidebarBlock)): ?>

代码语言:javascript
复制
        <section class="widgetbox">
            <h3><?php _e('网站统计'); ?></h3>
            <ul class="blogroll">
                <?php Typecho_Widget::widget('Widget_Stat')->to($stat); ?>
                <li><?php _e('文章总数:'); ?><?php $stat->publishedPostsNum() ?></li>
                <li><?php _e('分类总数:'); ?><?php $stat->categoriesNum() ?></li>
                <li><?php _e('评论总数:'); ?><?php $stat->publishedCommentsNum() ?></li>
                <li><?php _e('页面总数:'); ?><?php echo $stat->publishedPagesNum + $stat->publishedPostsNum; ?></li>
        </section>

加载时间

在 functions.php 中加入以下代码:

function timer_start() {

代码语言:javascript
复制
global $timestart;
$mtime     = explode( ' ', microtime() );
$timestart = $mtime[1] + $mtime[0];
return true;

}timer_start();function timer_stop( display = 0, precision = 3 ) {

代码语言:javascript
复制
global $timestart, $timeend;
$mtime     = explode( ' ', microtime() );
$timeend   = $mtime[1] + $mtime[0];
$timetotal = number_format( $timeend - $timestart, $precision );
$r         = $timetotal < 1 ? $timetotal * 1000 . " ms" : $timetotal . " s";
if ( $display ) {
    echo $r;
}
return $r;

}

然后,在模板中引用:

<?php echo timer_stop();?>

批量替换文章内容中的旧地址

修改网站地址后,会有好多附件地址不变,附一个SQL语句,应用于phpmyadmin,批量修改:

代码语言:javascript
复制
UPDATE `typecho_contents` SET `text` = REPLACE(`text`,'旧域名地址','新域名地址');

同样可以替换其它表的内容:

代码语言:javascript
复制
UPDATE `typecho_golinks` SET `target` = REPLACE(`target`,'a.b','b.c');

替换其它内容:

UPDATE typecho_fields SET str_value = REPLACE(str_value,'hostkvm-com','hostkvm-vpsmm');

缩略图调用 img 字段

自动调用img字段内容

<?php if (array_key_exists('img',unserialize(this->content, imgCount = count(matches[0]);if(imgCount >= 1){matches2;echo <<<Html{

自定义 面包蟹 导航

自定义 搜索或分类 面包蟹 部分

代码语言:javascript
复制
<?php $this->archiveTitle(array(
            'category'  =>  _t('分类 %s 下的文章'),
            'search'    =>  _t('包含关键字 %s 的文章'),
            'tag'       =>  _t('标签 %s 下的文章'),
            'author'    =>  _t('%s 发布的文章')
        ), '', ''); ?>

自定义 header 部分

对header部分,进行新定义:

代码语言:javascript
复制
<?php $this->header('wlw=&xmlrpc=&rss2=&atom=&rss1=&template=&pingback=&generator'); ?>

首页摘要自动截取样式

带图片输出

代码语言:javascript
复制
<?php 
if(preg_match('/<!--more-->/',$this->content)||mb_strlen($this->content, 'utf-8') < 270)
{
$this->content('阅读全文...');
}
else
{ 
$c=mb_substr($this->content, 0, 270, 'utf-8');
echo $c.'...';
echo '</br><p class="more"><a href="',$this->permalink(),'" title="',$this->title(),'">阅读全文...</a></p>';
}
?>

不带图片输出:

代码语言:javascript
复制
<?php 
if(preg_match('/<!--more-->/',$this->content)||mb_strlen($this->content, 'utf-8') < 270)
{
$this->content('阅读全文...');
}
else
{ 
$c=mb_substr($this->content, 0, 270, 'utf-8');
$c=preg_replace("/<[img|IMG].*?src=[\'\"](.*?(?:[\.gif|\.jpg|\.jpeg|\.png|\.tiff|\.bmp]))[\'|\"].*?[\/]?>/","",$c);
echo $c.'...';
echo '</br><p class="more"><a href="',$this->permalink(),'" title="',$this->title(),'">阅读全文...</a></p>';
}
?>

截取代码部分输出:

代码语言:javascript
复制
<?php 
if(preg_match('/<!--more-->/',$this->content)||mb_strlen($this->content, 'utf-8') < 270)
{
$this->content('阅读全文...');
}
else
{ 
$c=mb_substr($this->content, 0, 270, 'utf-8');
$c=preg_replace("/<[img|IMG].*?src=[\'\"](.*?(?:[\.gif|\.jpg|\.jpeg|\.png|\.tiff|\.bmp]))[\'|\"].*?[\/]?>/","",$c);
if(preg_match('/<pre>/',$c))
{
echo $c,'</code></pre>','...';;
}
else
{
echo $c.'...';
}
echo '</br><p class="more"><a href="',$this->permalink(),'" title="',$this->title(),'">阅读全文...</a></p>';
}
?>

在文章中插入广告

其实就是判断查找文章的第一个p,然后,插入代码,放到functions里使用即可。

代码语言:javascript
复制
function themeInit($archive) {
   
   // 判断是否是文章,如果是就插入广告
   $ad_code = '<div>这是你的广告</div>';
   if ($archive->is('single')) {
        $archive->content = prefix_insert_after_paragraph( $ad_code, 2, $archive->content );;
    }
}

// 插入广告所需的功能代码
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
   $closing_p = '</p>';
   $paragraphs = explode( $closing_p, $content );
   foreach ($paragraphs as $index => $paragraph) {
      if ( trim( $paragraph ) ) {
         $paragraphs[$index] .= $closing_p;
      }
      if ( $paragraph_id == $index + 1 ) {
         $paragraphs[$index] .= $insertion;
      }
   }
   return implode( '', $paragraphs );
}

文章字数统计

在functions.php中写入代码:

代码语言:javascript
复制
function  art_count ($cid){
$db=Typecho_Db::get ();
$rs=$db->fetchRow ($db->select ('table.contents.text')->from ('table.contents')->where ('table.contents.cid=?',$cid)->order ('table.contents.cid',Typecho_Db::SORT_ASC)->limit (1));
echo mb_strlen($rs['text'], 'UTF-8');
}

在模板中调用:

代码语言:javascript
复制
<?php echo art_count($this->cid); ?>

文章页TAG只输出名字

文章页面,只输入tag的名字,而没有链接。

代码语言:javascript
复制
<?php $this->tags(', ', false, 'none'); ?>

获取文章图片数量

/*

  • 获取图片数量 **/
代码语言:javascript
复制
function hui_post_imgNum($content){
   $output =
   preg_match_all("/\]*>/i", $content,$matches);
   $cnt = count( $matches[1] );
       return $cnt;
}

调用方法:

代码语言:javascript
复制
<?php echo ''.hui_post_imgNum($this->content).'' ; ?>

自定义首页或独立页面

代码语言:javascript
复制
<?php
/**
 * 自定义首页模板
 *
 * @package index
 */

<?php
/**
 * 自定义页面模板
 *
 * @package custom
 */

显示最新贴子图标

例如24小时内发布的贴,需要一个标志来完成。这里是用判断输入特殊字符,再用CSS判断完成的。

代码语言:javascript
复制
/**
 * 判断时间区间
 * 
 * 使用方法  if(timeZone($this->date->timeStamp)) echo 'ok';
 */
function timeZone($from){
$now = new Typecho_Date(Typecho_Date::gmtTime());
return $now->timeStamp - $from < 24*60*60 ? true : false;
}

以上代码,加入到 functions.php 中,然后,在 index.php 中使用如下调用:

代码语言:javascript
复制
<?php if(timeZone($this->date->timeStamp)) echo ' new'; ?>

注:这样就会输出一个new的文字,可应用于class里,然后,自定义输出背景图片等。


  1. >
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年05月13日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档