Typecho模板的header页面片

程潇
2018-09-10 / 0 评论 / 518 阅读 / 正在检测是否收录...

Typecho模板中,一般都会有header.php页面片,用于将页面公共头部抽离出来,减少代码冗余。

header页面片中,可以包含html的head标签和网站导航栏nav。

    <!DOCTYPE html>
    <html>
    <head>
    <title>这是一段标题</title>
    <!-- 各种meta-->
    <meta charset="UTF-8"/>
    <!-- 各种css资源-->
    <link rel="stylesheet" href="http://ltest.typechodev.com/usr/themes/BootMantic/css/form.css"/>
    </head>
    <nav>
        <ul><li></li></ul>
    </nav>

网站标题

$this->options->title()
<title></title>标签中包含网站的标题,可以使用`$this->options->title()`直接输出。

    <title><?php $this->options->title(); ?></title>

$this->archiveTitle()

此api可用于快速输出文章页的标题。一般用法为:

    <title><?php $this->archiveTitle(array(
        'category'  =>  _t('分类 %s 下的文章'),
        'search'    =>  _t('包含关键字 %s 的文章'),
        'tag'       =>  _t('标签 %s 下的文章'),
        'author'    =>  _t('%s 发布的文章')
         ), '', ' - '); ?>
    </title>

以上代码表示,如果当前页面是category,则输出“分类 %s 下的文章”,如果当前页面是search,那么输出“包含关键字 %s 的文章”,其他类推。

archiveTitle()函数的第二个参数是$before,第三个是$after,即分别会拼接在第一个参数的前面和后面。譬如在文章页“这是一篇测试文章”的文章下,

    $this->archiveTitle(array(
    'post'  =>  _t('这是一篇名为 %s 的文章'),
    ),'前面的文字','后面的文字');

会输出:

前面的文字这是一篇名为 这是一篇测试文章 的文章后面的文字

以上两种输出标题的方式,各位筒子根据自己的需要使用,或者配合is函数使用

资源加载

加载html静态资源,主要是能够在模板中找到资源所在的位置。

$this->options->themeUrl()

themeUrl()用于加载当前模板下的资源文件。假如当前模板未usrthemesdemo,模板目录下存在资源文件css\style.css,那么

    <link rel="stylesheet" href="<?php $this->options->themeUrl('css/style.css'); ?>">

将输出:

    <link rel="stylesheet" href="htt://your-site.com/usr/themes/demo/css/style.css">

同理,通过这种方式可以定位js等其他有需要的资源文件。

$this->options->adminUrl()

有时,你可能需要用到admin目录下的资源文件,譬如typecho后台的js/html5shiv.js就是一个很优秀的库。如果你的模板需要引用此文件,怎么加载呢?很简单,使用相类似的adminUrl()即可。

    <script src="<?php $this->options->adminUrl('js/html5shiv.js'); ?>"></script>

如需要了解更多xxxUrl方式,请移步到《Typecho中的Option》,这里将做详细介绍。

搜索引擎优化

header中的SEO,主要是优化title输出(见上文),另外还可以在head标签中增加description和keywords字段。
譬如:

通过$this->header方式输出

Typecho自带的$this->header()函数,可以胜任绝大部分情况,同时,如无特殊需求,也建议使用这种方式输出,是因$this->header()函数考虑了很多情况,适合大部分的场景。
header()的定义可参考\var\Widget\Archive.php

    <?php $this->header()?>

一般会输出:

    <meta name="description" content="Just So So ..." />
    <meta name="keywords" content="typecho,php,blog" />
    <meta name="generator" content="Typecho 1.0/14.10.10" />
    <meta name="template" content="BootMantic" />
    <link rel="pingback" href="http://ltest.typechodev.com/index.php/action/xmlrpc" />
    <link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://ltest.typechodev.com/index.php/action/xmlrpc?rsd" />
    <link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://ltest.typechodev.com/index.php/action/xmlrpc?wlw" />
    <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="http://ltest.typechodev.com/index.php/feed/" />
    <link rel="alternate" type="application/rdf+xml" title="RSS 1.0" href="http://ltest.typechodev.com/index.php/feed/rss/" />
    <link rel="alternate" type="application/atom+xml" title="ATOM 1.0" href="http://ltest.typechodev.com/index.php/feed/atom/" />

自定义输出

当然,如果你的需求比较特殊,系统自带的header()函数无法满足你的需求,那么可以使用api自己拼接html代码。

keywords

<meta name="keywords" content="<?php $this->keywords() ?>" />

将会输出后台配置的关键字信息。

keywords()函数接受两个参数:

    public function keywords($split = ',', $default = '')
    {
        echo empty($this->_keywords) ? $default : str_replace(',', $split, htmlspecialchars($this->_keywords));
    }

第一个参数是分隔符,第二个参数是默认值,即如果后台没有配置关键字,那么会输出默认信息。

description

    <meta name="description" content="xxx" />

举例:

    <?php if(!$this->is('index')):?>
        <meta name="description" content="TypechoDev是一个专注于Typecho模板制作、插件开发、TE安全等技术的站点..." />
    <?php elseif(!$this->is('post')):?>
        <meta name="description" content="<?php $this->options->description() ?>" />
    <?php esle: ?>
        <meta name="description" content="<?php $this->excerpt(200) ?>" />
    <?php endif?>

以上例子,即可根据不同页面输出不同的description内容。

注意:如果没有使用$this->header()的方式,那么插件挂载点headerOptions将失效。

0

评论 (0)

取消
网站已运行 00000000

Powered by Typecho ※ Theme is RST网络


CDN Sponsor Server provider Google Analytics Google Analytics