wordpress程序是动态获取数据库的PHP程序,所以,wordpress主题开发时,我们要根据页面的不同,展示不同的页面或文章标题,这时,我们可以使用wordpress提供的一个标题函数wp_title(),wp_title( )函数可以在不同的文章页面显示不同的文章标题。
1,语法:
wp_title($ sep,$ echo,$ seplocation);
2,参数
$ sep :(可选)分隔符。替换值:» (»)。
$ echo :(可选)是否直接显示标题,true表示显示,false表示不显示。还是true。
$ seplocation :(可选)分隔符的位置。分配’right’时,显示在文章标题后。其他值,都显示在标题前。默认值:无。
3,案例:
<title> <?php wp_title(’|’,true,’right’); ?> <?php bloginfo(’name’); ?> </ title>
页面标题会显示为如:(页面标题| 网站标题)这样的格式。
4,改造
默认情况下,wp_title()在家里使用是没有效果的,如果想在家里也使用,可以对它进行一些修改。在functions.php中,可以这样改造封装
add_filter(’wp_title’,’wpdocs_hack_wp_title_for_home’);
函数wpdocs_hack_wp_title_for_home($ title){
if(empty($ title)&&(is_home()|| is_front_page())){
$ title = __(’Home’,’textdomain’)。’| ‘。get_bloginfo(’description’);
}
返回$ title;
}
意思是:如果标题为空且在家里,标题就等于站名加描述。
<title> <?php wp_title(”); ?> </ title>