调用文章、站点标题

作者: admin 分类: wordpress主题制作教程 发布时间: 2017-09-12 15:35

调用标题的代码如下:

<title><?php wp_title(‘-‘, true, ‘right’); ?></title>

其中,加红的部分,就是调用的标题内容。

wordpress中有默认的标题函数,但因为不是那么理想,所以需要对函数进行下加工,可以在functions.php中加入下面的代码,实现标准格式化的站点标题:

/**
* 想要wp_title()函数实现,访问首页显示“站点标题-站点副标题”
* 如果存在翻页且正方的不是第1页,标题格式“标题-第2页”
* 当使用短横线-作为分隔符时,会将短横线转成字符实体–
* 而我们不需要字符实体,因此需要替换字符实体
* wp_title()函数显示的内容,在分隔符前后会有空格,也要去掉
*/

add_filter(‘wp_title’, ‘lingfeng_wp_title’, 10, 2);
function lingfeng_wp_title($title, $sep) {
global $paged, $page;

//如果是feed页,返回默认标题内容
if ( is_feed() ) {
return $title;
}

// 标题中追加站点标题
$title .= get_bloginfo( ‘name’ );

// 网站首页追加站点副标题
$site_description = get_bloginfo( ‘description’, ‘display’ );
if ( $site_description && ( is_home() || is_front_page() ) )
$title = “$title $sep $site_description”;

// 标题中显示第几页
if ( $paged >= 2 || $page >= 2 )
$title = “$title $sep ” . sprintf( ‘第%s页’, max( $paged, $page ) );

//去除空格,-的字符实体
$search = array(‘&#8211;’, ‘ ‘);
$replace = array(‘-‘, ”);
$title = str_replace($search, $replace, $title);

return $title;
}

然后对于在header.php模板中引用wp_title()函数调用标题即可。

关于wp_title()函数用法:

<?php wp_title( $sep, $echo, $seplocation ); ?>

 

参数详解:

$sep:分隔符;
$echo:是否显示;true为显示,一般都是true
$seplocation:分隔符所在位置(左还是右,只接受’right’,如果不是right自动判定为左)

相关文章

发表评论

邮箱地址不会被公开。 必填项已用*标注

Powered by 草根站长笔记 © 2015-2020 草根站长笔记 Inc.版权所有,禁止转载