徐伟轩博客-爱写歌的程序员思考和科普的日常自媒体

统计
TypechoJoeTheme

Typecho开发常用的标签及代码整理

徐伟轩博主
2021-10-03 09:32
/
0 评论
/
492 阅读
/
899 字
10月03

国内众多的CMS中,Typecho更加简介小巧,但是又不失功能性。今天徐伟轩就整理下经常使用到的Typecho的标签及代码设置,算是记录下方便后期找到参考。

一、基础设置对SEO更友好
【博客首页】博客名称
【独立页面】页面标题 - 博客名称
【分类页面】分类名称 - 博客名称
【标签页面】Tag: 标签 - 博客名称
【存档页面】存档: XXXX年XX月 - 博客名称
【其它(日志页)】页面标题

<div>
<title>
<?php if ($this->is('index')){$this->options->title();}
elseif($this->is('page')){$this->archiveTitle('','',' - ');$this->options->title();}
elseif($this->is('category')){$this->archiveTitle('','',' - ');$this->options->title();}
elseif($this->is('tag')){$this->archiveTitle('','Tag: ',' - ');$this->options->title();}
elseif($this->is('archive')){$this->archiveTitle('年','存档: ','月 - ');$this->options->title();}
else{$this->archiveTitle('','','');}?>
</title>
<title><?php $this->archiveTitle('', '', ' - '); ?><?php $this->options->title(); ?>-<?php $this->options->description(); ?></title>
</div>

二、在每篇文章固定位置调出当前文章的TAGS标签

<div>
<?php $this->tags(',', true, 'none'); ?>
Select CodeCopy
</div>

一般我们会采用这个方法,我们来解读具体的使用方法。

1、 ',' - 引号中的英文逗号是每个标签的隔开字符,我们也可以使用 span、div 等自定义样式隔开。比如有些朋友喜欢做多色标签的,那就需要调整。
2、true - 这个表示标签是否带连接,带就用true,不带就用false
3、none - 表示没有标签时候留空,当然我们也可以设置其他参数。
三、Typecho 热门标签调用及标签云
1、有些模板侧边栏会条用到标签,有些会调用热门标签,这就要用到如下代码了,实际上就是调用标签加上排序
代码如下:

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

解析一下大致意思如下:

'sort' => 'count' 应该是表示按标签数量排序;

'ignoreZeroCount' => true 应该是表示过滤掉数量为0的空标签;

'limit' => 20 应该是表示调用标签数量为20个;
至于标签的样式,由CSS控制,自行修改。

至于标签云调用请看如下代码:

<div>
<div class="widget">
<h3><?php _e('所有标签'); ?></h3>
            <ul class="cate">
                <?php $this-&gt;widget(&#39;Widget_Metas_Tag_Cloud&#39;)-&gt;to($tags); ?>   
               <?php while($tags->next()): ?>   
               <li><a rel="tag" href="<?php $tags-&gt;permalink(); ?&gt;&quot;&gt;&lt;?php $tags->name(); ?></a></li>
               <?php endwhile; ?>
<div class="clear"></div>
            </ul>
</div>
</div>

四、侧边栏每月归档后面显示文章数

<div>
<?php $this->widget('Widget_Contents_Post_Date', 'type=month&format=Y年m月')
->parse('<li><a href="{permalink}">{date}</a> <span id="ignore">({count})</span></li>'); ?>
</div>

五、用户复制你文章的时候,带版权说明
其实也就是使用如下代码

<div>
<script type="text/javascript">
document.body.oncopy = function () 
{ setTimeout( function () { var text = clipboardData.getData("text"); if (text) 
{ text = text + "\r\n本篇文章来源于<?php $this->options->title(); ?>|
<?php $this->options->siteUrl(); ?>,
原文链接:"+location.href; clipboardData.setData("text", text); } }, 100 ) }
</script>
</div>

六、最新文章

<div>
<?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; ?>
</div>

七、全部日志数量

<div>
<?php $stat = Typecho_Widget::widget('Widget_Stat') ;echo ".$stat->PublishedPostsNum."; ?>
使用这个方法,可以自定义首页的文章显示条数,以及自定义分类文章显示:
function themeInit($archive) {
    if ($archive->is('index')) {
        $archive->parameter->pageSize = 10; // 自定义条数
    }
}
</div>

或者:

<div>
function themeInit($archive) {
    if ($archive->is('category', 'default')) {
        $archive->parameter->pageSize = 10; // 自定义条数
    }
}
</div>

八、相关内容调用

<div>
<?php $this->related(5)->to($relatedPosts); ?>
<?php while ($relatedPosts->next()): ?>
<a href=”<?php $relatedPosts->permalink(); ?>” title=”
<?php $relatedPosts->title(); ?>”><?php $relatedPosts->title(); ?></a>
<small><strong><?php $relatedPosts->author(); ?></strong> post in
<?php $this->date('Y-m-d H:i:s'); ?></small>
<?php endwhile; ?>
</div>


九、日期归档

<div>
<?php $this->widget('Widget_Contents_Post_Date', 'type=month&format=F Y')->parse('<li><a href=”{permalink}”>{date}</a> ({count})</li>'); ?>
</div>

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

<div>
function showThumbnail($widget) {
$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;
} }
Select CodeCopy
</div>

调用方式:

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

十一、友情链接 (需要装插件)

<div>
<?php PageToLinks::output('links', 'h3', 'ul'); ?>
</div>

十二、文章统计代码实现

//统计调用实现 itbulu.com

<div>
function 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'];
}
Select CodeCopy
</div>

调用:

<div>
<?php get_post_view($this) ?>
Select CodeCopy
</div>

需要的位置调用。
十三、分页的代码

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

十四、搜索代码

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

十五、全部文章列表代码
可以应用在任何地方:

<div><?php $this->widget('Widget_Contents_Post_Recent', 'pageSize=10000')->parse('<li>{year}-{month}-{day} : <a href="{permalink}">{title}</a></li>'); ?></div>

全部标签集列表

<div><?php $this->widget('Widget_Metas_Tag_Cloud')
                ->to($taglist); ?><?php while($taglist->next()): ?>
<li><a href="<?php $taglist->permalink(); ?>" title="<?php $taglist->name(); ?>"><?php $taglist->name(); ?></a></li>
<?php endwhile; ?></div>

十六、侧栏评论忽略博主评论
博客虽然是博主写的,但是点评的主场还是网站访客。

<div><?php $this->widget('Widget_Comments_Recent','ignoreAuthor=true')->to($comments); ?>

typecho自动显示摘要,158为自动摘要的字数的一半<p><?php $this->excerpt(158); ?></p>
<p class="more"><a href="<?php $this->permalink() ?>" rel="bookmark" title="<?php $this->title() ?>">阅读全文</a></p></div>

评论输出:

<div><?php $comments->author(true); ?>//输出评论人昵称,参数true为带链接的,false为不带链接的;
//ps.其实还有一个参数,类型也是布尔,作用是是否带noFollow
<?php $comments->excerpt(26, '...'); ?>//输出评论摘要,第一个参数是要截取的长度,第二个是截取后的字符串;
<?php $comments->permalink(); ?>//获取当前评论链接,木有参数;
<?php $comments->title(); ?>//获取当前评论标题,同样木有参数;
<?php $comments->dateWord(); ?>//输出词义化日期,就是输出“3小时前”、“三天前”之内的;
<?php $comments->gravatar(); ?>//调用gravatar输出用户头像,有两个参数,第一个是头像尺寸,默认是32,第二个是默认输出的头像。</div>

更多内容的话,后期想到再补充到下方。

本文内容整理自互联网 http://ee19.com/blog/32.html

赞(0)
赞赏
感谢您的支持,我们会继续努力哒!
版权:

徐伟轩博客-爱写歌的程序员思考和科普的日常自媒体

本文链接:

https://letus.top/archives/396.html(转载时需注明本文出处及文章链接)

如无特别注明,本站内容为原创。

如需转载或刊登,请联系我们获得授权。

评论 (0)
本篇文章评论功能已关闭

亲爱的朋友

一切伟大,源于勇敢的开始。

人生倒计时

今日已经过去小时
这周已经过去
本月已经过去
今年已经过去个月

标签云