WordPress主题开发实例:格式化标题
2017-05-03 23:13
495 查看
页面使用:
functions.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title><?php wp_title('-',true,'right');?></title> ......
functions.php
<?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('–', ' '); $replace = array('-', ''); $title = str_replace($search, $replace, $title); return $title; } ?>
相关文章推荐
- WordPress主题开发实例:get_term_by()获取指定分类链接
- WordPress主题开发实例:获取当前分类的文章列表
- WordPress主题开发实例:产品展示
- WordPress主题开发实例:显示最新发表文章
- WordPress主题开发:WP_Query使用分页实例
- WordPress主题开发实例:利用侧边栏工具显示联系方式
- wordpress主题开发教程手记:短代码功能开发实例
- WordPress主题开发实例:查询单篇文章
- WordPress主题开发:截取标题或内容
- WordPress主题开发实例:根据不同分类使用不同模板
- wordpress主题开发教程手记:wp-login.php登录页面优化
- WordPress 主题开发 - (十) Single、Post及404模板 待翻译
- WordPress 主题开发 - (五)WordPress 主题模板及目录结构 待翻译
- WordPress 主题开发 - (七) 让主题更安全 待翻译
- WordPress 主题开发 - (四) 创建WordPress的主题HTML结构 待翻译
- wordpress主题开发常用缓存API的讲解与调用方案
- WordPress 主题开发 - (九) Index模板 审校
- WordPress 主题开发 - (二) 理解主题 待翻译
- WordPress 主题开发 - (一) 前言 待翻译
- WordPress 主题开发 - (十二) Search模板与Page模板 待翻译