header 与 footer
类似于 WordPress 的 wp_head() 和 wp_footer() 钩子,Typecho 也提供了类似的插件钩子(如 header() 和 footer() 方法),允许插件在头部或尾部插入内容,当然有时候不想采用插件的形式,直接将语句写在主题里也是能够实现效果的。这两个函数定义在 var\Widget\Archive.php
文件内,有需要的可以去这里查看源码。
代码摘抄
header()
部分代码摘录:
/**
* 输出头部元数据
*
* @access public
* @param string $rule 规则
* @return void
*/
public function header($rule = NULL)
{
$rules = array();
$allows = array(
'description' => htmlspecialchars($this->_description),
'keywords' => htmlspecialchars($this->_keywords),
'generator' => $this->options->generator,
'template' => $this->options->theme,
'pingback' => $this->options->xmlRpcUrl,
'xmlrpc' => $this->options->xmlRpcUrl . '?rsd',
'wlw' => $this->options->xmlRpcUrl . '?wlw',
'rss2' => $this->_feedUrl,
'rss1' => $this->_feedRssUrl,
'commentReply' => 1,
'antiSpam' => 1,
'atom' => $this->_feedAtomUrl
);
/** 头部是否输出聚合 */
$allowFeed = !$this->is('single') || $this->allow('feed') || $this->_makeSinglePageAsFrontPage;
if (!empty($rule)) {
parse_str($rule, $rules);
$allows = array_merge($allows, $rules);
}
$allows = $this->pluginHandle()->headerOptions($allows, $this);
$title = (empty($this->_archiveTitle) ? '' : $this->_archiveTitle . ' » ') . $this->options->title;
footer()
函数的代码:
/**
* 支持页脚自定义
*
* @access public
* @return void
*/
public function footer()
{
$this->pluginHandle()->footer($this);
}
通过header()加载资源
这是小鸟数据 August 主题的资源加载类,供参考:
<?php
/*==========
August Setup
==========*/
if ( ! class_exists( 'Aug_Setup' ) ) {
class Aug_Setup {
private static $instance;
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
private $assetsCss=[
'SwiperCss' => 'swiper@3.4.2/swiper.min.css',
'FontawesomeCss' => 'font-awesome@4.7.0/font-awesome.min.css',
'FancyboxCss' => 'fancybox@3.5.7/jquery.fancybox.min.css',
'PrismCss' => 'prism/prism.css'
];
private $assetsJs=[
'JqueryJs' => 'jquery@1.12.4/jquery.min.js',
'SwiperJs' => 'swiper@3.4.2/swiper.jquery.min.js',
'JqcloudJs' => 'jqcloud@1.0.4/jqcloud-1.0.4.min.js',
'LazyloadJs' => 'lazyload@1.9.7/jquery.lazyload.min.js',
'FancyboxJs' => 'fancybox@3.5.7/jquery.fancybox.min.js',
'QrcodeJs' => 'qrcode@1.0/jquery.qrcode.min.js',
'PrismJs' => 'prism/prism.js'
];
private function __construct() {
$this->add_db_keys();
Typecho_Plugin::factory('Widget_Archive')->header = [ $this , 'enqueue_styles'];
Typecho_Plugin::factory('Widget_Archive')->header = function(){
echo '<link type="text/css" rel="stylesheet" href="' . AUG_THEME_URI
. '/style.css'
. '?ver=' . AUG_THEME_VER . '"/>' . "\n";
};
Typecho_Plugin::factory('Widget_Archive')->header = [ $this , 'enqueue_scripts'];
Typecho_Plugin::factory('Widget_Archive')->header = function(){
if( Helper::options()->MathjaxJs ){
echo '<script type="text/javascript" src="' . Helper::options()->MathjaxJs . '"></script>' . "\n";
}
echo '<script type="text/javascript" src="' . AUG_THEME_URI
. '/assets/js/main.js'
. '?ver=' . AUG_THEME_VER . '"></script>' . "\n";
};
}
/* 添加数据库字段 */
public function add_db_keys() {
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
try {
if (!array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')->page(1, 1)))) {
$db->query('ALTER TABLE `' . $prefix . 'contents` ADD `views` INT DEFAULT 0;');
}
if (!array_key_exists('agree', $db->fetchRow($db->select()->from('table.contents')->page(1, 1)))) {
$db->query('ALTER TABLE `' . $prefix . 'contents` ADD `agree` INT DEFAULT 0;');
}
} catch (Exception $e) {}
}
/* 添加 Css 依赖 */
public function enqueue_styles() {
foreach ($this->assetsCss as $key => $value) {
if (Helper::options()->$key) {
$assetsUrl = Helper::options()->$key;
} else {
$assetsUrl = AUG_LIB_URI . '/' . $value;
}
echo '<link type="text/css" rel="stylesheet" href="' . $assetsUrl . '"/>' . "\n";
}
}
/* 添加 Js 依赖 */
public function enqueue_scripts() {
foreach ($this->assetsJs as $key => $value) {
if (Helper::options()->$key) {
$assetsUrl = Helper::options()->$key;
} else {
$assetsUrl = AUG_LIB_URI . '/' . $value;
}
echo '<script type="text/javascript" src="' . $assetsUrl . '"></script>' . "\n";
}
}
}
}
Aug_Setup::get_instance();
文章页的关键词与摘要
typecho会自动提取文章的一部分内容作为文章摘要,如果想要自定义摘要以及关键词,那么除了在文章页直接输出自定义内容外,还需要屏蔽掉 header()
函数内的默认内容,以下是 joe 主题这一部分的代码实现,有需要的可以参考代码进行自定义:
<?php if ($this->is('single')) : ?>
<meta name="keywords" content="<?php echo $this->fields->keywords ? $this->fields->keywords : htmlspecialchars($this->_keywords); ?>" />
<meta name="description" content="<?php echo $this->fields->description ? $this->fields->description : htmlspecialchars($this->_description); ?>" />
<?php $this->header('keywords=&description='); ?>
<?php else : ?>
<?php $this->header(); ?>
<?php endif; ?>