OneStep

typecho轻量博客详解!

Toc目录

typecho开源博客内部 链接 注解

<?php $this->options->siteUrl();?> 主页(控制台-基础设置-站点地址)这个网址由个人后台设置
如果源码中css和js路径引用了上面的代码,后台中请切莫修改,保持和实际网站主页一致
<?php $this->options->rootUrl(); ?> 网站主页

<?php $this->options->description() ?> 站点说明描述

<?php $this->options->title(); ?> 标题
<?php $this->title(); ?> 文章标题
<a href="<?php $this->permalink(); ?>"><?php $this->title(); ?></a> 带跳转链接的文章标题
<?php $this->title(20, '...'); ?> 参数20表示文章标题显示字数最大值(包含其二个参数一起总20个字数,这样设置实际效果就是文章标题字数最多显示17个 超出部分以...代替)
备注:字母同汉字一样 也算一个

<?php $this->user->screenName(); ?> 登陆的用户名(登录状态才能显示)

<?php $this->options->themeUrl('lib/jquery.min.js'); ?> 外观主题 所在的路径地址
备注:最好还是使用相对路径 来引入css样式和js脚本 这样更独立不受影响
/usr/themes/default主题目录/lib/jquery.min.js
因为<?= $this->options->themeUrl; ?>网址和<?php $this->options->siteUrl();?>主页是关联的

<?php $categorys->mid(); ?> 分类对应 mid
需要 导入 <?php $this->widget('Widget_Metas_Category_List')->to($categorys); ?> 方能使用

<?php $categories->slug(); ?> 疑是文章分类 缩略名

<?php $options->adminUrl(); ?> 后台管理
<?php $this->options->adminUrl(); ?> 同上
及时在服务器空间吧后台目录admin修改成其他目录名称也不影响
<?php $this->options->adminUrl('login.php'); ?> 后台登陆地址
<?php $options->adminUrl(write-post.php); ?> 后台管理-撰写新文章

<?php $this->category(','); ?> 显示文章所属 分类名
<?php $this->category(""); ?> 同上
<?php $this->category(',', false); ?> 纯文字分类名称,false代表 不带链接

<?php $this->tags(' ', true); ?> 显示文章所属 标签
<?php $this->tags(',', true, ''); ?> 同上
中间参数true 代表含义 以链接形式
false 不以链接形式
<?php if( count($this->tags) == 0 ): ?> 判断标签是否存在

标签云

<?php $this->widget('Widget_Metas_Tag_Cloud', 'ignoreZeroCount=1&limit=30')->to($tags); ?>
<ul class="tags-list">
<?php while($tags->next()): ?>
<li><a href="<?php $tags->permalink(); ?>" title='<?php $tags->name(); ?>'>
<?php $tags->name(); ?></a></li>
<?php endwhile; ?>
</ul>

备注:limit=30 标签数量;desc:是否降序输出;ignoreZeroCount:忽略文章数为 0 的;sort:排序方式为 mid;
0为假(否) ,1为真(是)
sort=mid&ignoreZeroCount=1&desc=0&limit=30

<?php $this->permalink() ?> 日期所对应的文章

<time datetime="<?php $this->date(); ?>" itemprop="datePublished"><?php $this->date(); ?></time> 文章所对应的日期

<?php echo gmdate('Y-m-d H:i', $this->modified + Typecho_Widget::widget('Widget_Options')->timezone); ?>
文章最后编辑时间
<?php echo date('Y-m-d H:i:s', $this->modified); ?> 文章最后更新时间

<?php $this->cid(); ?> 当前文章的id

发布于<time pubdate="true" datetime="<?php $this->date(); ?>" data-humantime="true"></time> 时间

<a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a> 文章作者及该作者名下文章链接
<?php $this -> author(); ?> 文章作者
< ?php $this->author->gravatar('40') ?> 作者头像 数字40代表头像的宽和高
<?php $this->author->url(); ?> 作者个人主页链接
<?php $this->author->mail(); ?> 作者的邮箱地址

<?php $this->options->feedUrl(); ?> RSS地址
<?php $this->options->commentsFeedUrl(); ?> 评论rss

<?php $this->need('component/post-card.php'); ?> 调用其他页面,括号内填入路径

<?php $this->content(); ?> 文章正文输出
<?php $this->content('阅读剩余部分...'); ?> 全部输出文章内容

<?php $this->excerpt(); ?> 显示文章摘要
<?php $this -> excerpt(100); ?> 只显示文章100字摘要
<?php $this->excerpt(100,'...'); ?> 100字后面内容隐藏以省略号...代替

<?php $this -> pageNav('&laquo;', '&raquo;', 3, "...", array('wrapTag' => 'section', 'itemTag' => 'span')); ?> 以 1 2 3 4 》 形式 首页显示文章列表 翻页按钮

<?php $this->pageNav('上一页', '下一页', 2); ?> 以1 2 3 ... 8 下一页 形式 首页文章列表 翻页按钮
数字2代表 前后相邻显示2个页码

<?php $this->pageLink('&laquo;Prev','prev'); ?><?php $this->pageLink('Next&raquo;','next'); ?>
首页文章列表 只显示 上一页(prev) 下一页(next) 两个按钮
显示效果: «Prev Next»

<?php $this->pageLink('? 新文章','prev'); ?> 显示新文章
<?php $this->pageLink('旧文章 ?','next'); ?> 显示旧文章

文章正文页面底部 前后文章跳转
<?php $this -> thePrev('%s','看完啦 (つд?)'); ?> 上一篇文章
<?php $this -> theNext('%s','看完啦 (つд?)'); ?> 下一篇
与 相邻文章 <?php theNextPrev($this); ?> 这个链接有什么差异

以箭头的图案形式 文章内容页面 显示上下翻页文章 按钮
<?php $this->thePrev('%s', NULL, array('title' => '&larr;', 'tagClass' => 'prev-content')); ?>
<?php $this->theNext('%s', NULL, array('title' => '&rarr;', 'tagClass' => 'next-content')); ?>

<?php if($this->user->hasLogin()): ?> 判断是否在用户登陆状态

<?php if($this->is('post') || $this->is('page')): ?> 判断是否是post页面或者page页面
页面类型:index post page 判断与或非 && ||

<?php $this->commentsNum(); ?> 显示当前文章评论数 私信 留言 显示
<?php $this->commentsNum('%d'); ?> 同上
<?php $this->commentsNum('', '1 条评论', '%d 条评论'); ?> 0条的时候不显示,1条的时候显示“1 条评论” ,n条的时候 显示“n 条评论”
<?php $this->commentsNum(_t('暂无评论'), _t('仅有 1 条评论'), _t('共有 %d 条评论')); ?>

<?php if ($this->sequence == 4): ?><li>我是逗比吗</li><?php endif; ?> 判断为本页第几篇文章输出对应内容 可应用于文章列表中插入广告 数字4代表第四篇文章 输出 <li>我是逗比吗</li> 每一页的第四条文章都输出该内容

<?php if($this->_currentPage == 1){ ?> 对分页 数字(第几页)操作,
示例:<?php if($this->_currentPage>1) echo $this->_currentPage; else echo 1;?> 当前页码大于1时,则输出页码 否则只输出1 输出当前所在页码
<?php echo ceil($this->getTotal() / $this->parameter->pageSize); ?> 输出总页码

未使用和不明白的部分链接
<?php $this->fields->fieldName ?> 调用自定义字段 怎么用 目前还不会!!! 博客撰写页面 能看到自定义字段
<?php echo $this->getDescription(); ?> 分类描述

自定义分类category下缩略名为test的文章显示数量

if ($archive->is('category', 'test')) {
$archive->parameter->pageSize = 25; // 自定义条数
}

将上面代码块放到functions.php的function themeInit($archive) { 上面语句放到此处 }
($archive->is('category', 'default')) 自定义分类文章显示条数

自定义首页显示文章条数

if ($archive->is('index')) {
$archive->parameter->pageSize = 20;
}

多个同时设置
($archive->is('index') || $archive->is('category') || || $archive->is('tag') $archive->is('search') || $archive->is('author') ) // 首页,分类,标签,搜索,作者
所有页面都统一设置显示文章固定条数 $archive->parameter->pageSize = 20; 保留这段语句 删除判断页面

其他的一些页面
$this->is('single') 文章内容页(包含post.php及page.php所有内容页面)
$this->is('archive') 归档页,譬如主页,分类文章页,标签文章页,日期归档文章页。
('archive',此处可以增加第二个参数进一步精准确定)
举例说明:$this->is('page','about') 关于页面

隐藏head区域的程序版本和模版名称
在header.php的文件head标签内 导入网站description和keywords <?php $this->header("generator=&template=&pingback=&xmlrpc=&wlw="); ?>

头部关键信息

<?php $this->keywords('_'); ?> //关键词 
<?php $this->options->title(); ?> //站点名称 
<?php $this->options->description(); ?> //站点描述 
<?php $this->archiveTitle(); ?> //标题 
<?php $this->options->themeUrl('ie.css'); ?> //模板路径 
<?php $this->options->siteUrl(); ?> //主页网址 
<?php $this->options->feedUrl(); ?> 
<?php $this->options->commentsFeedUrl(); ?> 
<?php $this->pageNav(); ?> //分页 
<?php $this->options->generator(); ?> //版本号 

网站数据统计

<?php Typecho_Widget::widget('Widget_Stat')->to($stat); ?>
文章总数:<?php $stat->publishedPostsNum() ?> 篇
分类总数:<?php $stat->categoriesNum() ?> 个
评论总数:<?php $stat->publishedCommentsNum() ?> 条
页面总数:<?php $stat->publishedPagesNum() ?> 个
当前作者的文章总数:<?php $stat->myPublishedPostsNum() ?> 篇
所有页面总数(文章页面和独立页面):<?php echo $stat->publishedPagesNum + $stat->publishedPostsNum; ?>

页面说明(预计需要使用的页面)

|||||||||||||||

页面 页面说明
必须页面 按重要程度排序
index.php 首页
header.php 头部
post.php 文章详细页
page.php 独立页面
footer.php 脚部
comments.php 留言
404.php 404访问出错页面
functions.php 功能存放 (后台设置外观)
archive.php 分类/搜索/作者/标签公用页面

PS:如果archive.php不存在,index.php也会作为通用页面,实现archive.php的工作。
很多主题都把archive的功能集成在主页面index.php里

其它文件
screenshot.png 主题缩略图
style.css 主题样式

其它用户自定义页面(根据需求自行增加删除)不是必须页面
sidebar.php 侧边栏目
search.php 搜索
author.php 作者自述
tag.php 标签
category.php 分类
avatars.php 留言墙
links.php 友情链接
file.php 归档页面
talk.php 说说功能z
talklist.php 说说页面
config.php 配置开关 (森の色的Story主题就有这个页面)在其他友人的博客主题中并未看到

个人diy修改
1.时钟调整为显示动态秒
2.文章页面内增加 “编辑”跳转到后台文章修改
3.增加站点图标

文件结构说明
文件名 作用 必须

  • 列表项目

style.css 主题样式文件
screenshot.png 主题模板预览图

文章浏览次数统计

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'];
}

将以上代码放入 模板functions.php文件中
然后在首页文件index.php或者文章正文文件post.php 的适当位置添加 <?php get_post_view($this); ?> 这行代码调用方法就可以了
网上的很多类似的typecho阅读次数统计的代码 都加了cookie验证。加ookie验证的很多时候刷新都不增加访问次数 现在大多浏览器会一直记录cookie 感觉不是很好
所以就随它 刷新一次增加一次吧 统计数据假一点也无所谓了 只是参考而已
备注:只有将<?php get_post_view($this); ?>这段代码放入post.php和page.php页面才能累加次数
如果page.php页面下没有存放这段代码,那么数据库后台中就不会累加page页面的浏览次数

显示网站动态运行时间

    //网站动态运行时间
function show_date_time() {
            if ($("#runtimespan").length > 0) {
                window.setTimeout("show_date_time()", 1000);
                BirthDay = new Date("07/15/2013 8:08:08");  //建站时间 格式:月/日/年 时:分:秒
                today = new Date();
                timeold = (today.getTime() - BirthDay.getTime());
                sectimeold = timeold / 1000;
                secondsold = Math.floor(sectimeold);
                msPerDay = 24 * 60 * 60 * 1000;
                e_daysold = timeold / msPerDay;
                daysold = Math.floor(e_daysold);
                e_hrsold = (e_daysold - daysold) * 24;
                hrsold = Math.floor(e_hrsold);
                e_minsold = (e_hrsold - hrsold) * 60;
                minsold = Math.floor((e_hrsold - hrsold) * 60);
                seconds = Math.floor((e_minsold - minsold) * 60);
                runtimespan.innerHTML = secondsold + " s";
                       runtimespan.title = daysold + "天" + hrsold + "小时" + minsold + "分" + seconds + "秒";         
            };
        }
        show_date_time();

把需要显示网站运行时间的位置 添加上 <span id="runtimespan"></span> 这条代码
默认以秒的形式显示输出
效果展示: “网站总运行时间:210230324 s”

文章字数统计

   // 增加文章字数统计
    function  artCount ($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));
        $text = preg_replace("/[^\x{4e00}-\x{9fa5}]/u","",$rs['text']);
        echo mb_strlen($text,'UTF-8');
    }

在需要的地方 调用<?php artCount($this->cid);?>汉字 就可以了

来源于折影轻梦

网站总访问浏览次数

//总访问量-查找views字段
function theAllViews()
{
    $db = Typecho_Db::get();
    $row = $db->fetchAll('SELECT SUM(VIEWS) FROM `typecho_contents`');
    echo number_format($row[0]['SUM(VIEWS)']);
}

或者使用下面这段代码

//浏览数统计
function get_sum_views(){
    $db = Typecho_Db::get();
    $prefix = $db->getPrefix();
    if (array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))){
        $pom = $db->fetchAll("SELECT SUM(VIEWS) FROM `" . $prefix . "contents` WHERE `type`='page' or `type`='post'");
        $num = number_format($pom[0]['SUM(VIEWS)'],0,'','');
        return $num;
    }else{
        return 0; 
    }
}

页面加载时间

//页脚加载耗时
function timer_start() {
        global $timestart;
        $mtime     = explode( ' ', microtime() );
        $timestart = $mtime[1] + $mtime[0];
        return true;
    }
    timer_start();
    function timer_stop( $display = 0, $precision = 3 ) {
        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;
}

常量集
define('__TYPECHO_DEBUG__',true); 打开 Debug 模式
define('__TYPECHO_SECURE__', true); 强制SSL
define('__TYPECHO_GRAVATAR_PREFIX__', 'https://xxx.com/avatar'); Avatar 前缀

内置常量(内部常量)
/* 定义根目录 /
define('__TYPECHO_ROOT_DIR__', dirname(__FILE__));
/* 定义插件目录(相对路径) /
define('__TYPECHO_PLUGIN_DIR__', '/usr/plugins');
/* 定义模板目录(相对路径) /
define('__TYPECHO_THEME_DIR__', '/usr/themes');
/* 后台路径(相对路径) /
define('__TYPECHO_ADMIN_DIR__', '/admin/');

支付宝打赏 微信打赏

免责声明:本站多数资源来源于互联网。少数原创内容为本人随意书写不对其负任何法律责任(均为键盘和鼠标自行敲击),部分有助于网友的内容仅供学习与交流。如有转载、盗链等其它行为产生对国家社会网络不良影响均与本人无关。
版权说明:未经博主授权禁止转载、摘编、复制或建立镜像。对既成事实本站将保留所有的权利。

      




发表评论

发表新评论

请输入本站名:

共有 2 条评论   

  1. 贝多叶 贝多叶博主 回复


    signature---在下只是初学者,不是所谓的大神。
  2. 1 1访客 回复


    signature---本站网址onestep.beiduoye.com
发送消息!
onestep
网站二维码
手机扫一扫,查看
.
贝多叶@聚合支付

真诚赞赏,手留余香

使用微信扫描二维码完成支付