在WordPress中,可以通过多种方式根据不同的页面调用不同的标题。这通常用于实现SEO优化、自定义页面标题或根据页面类型显示不同的标题内容。
使用wp_title函数
wp_title函数用于在HTML的title标签中输出页面标题。你可以通过修改主题的header.php文件来实现自定义标题。
<title><?phpif (is_home()) {echo '首页标题 - 网站名称';} elseif (is_single()) {echo single_post_title('', false) . ' - 网站名称';} elseif (is_page()) {echo the_title() . ' - 网站名称';} elseif (is_category()) {echo single_cat_title('', false) . ' - 网站名称';} elseif (is_tag()) {echo single_tag_title('', false) . ' - 网站名称';} else {echo '默认标题 - 网站名称';}?>
</title>
使用wp_title过滤器
如果你不想直接修改header.php文件,可以通过wp_title过滤器在functions.php中自定义标题。
function custom_wp_title($title, $sep) {global $page, $paged;if (is_home()) {$title = '首页标题 - 网站名称';} elseif (is_single()) {$title = single_post_title('', false) . ' - 网站名称';} elseif (is_page()) {$title = the_title('', '', false) . ' - 网站名称';} elseif (is_category()) {$title = single_cat_title('', false) . ' - 网站名称';} elseif (is_tag()) {$title = single_tag_title('', false) . ' - 网站名称';} else {$title = '默认标题 - 网站名称';}return $title;
}
add_filter('wp_title', 'custom_wp_title', 10, 2);
根据页面模板调用标题
如果你使用了自定义页面模板,可以通过模板文件中的条件判断来调用不同的标题。
<?php
if (is_page_template('template-contact.php')) {echo '<h1>联系我们</h1>';
} elseif (is_page_template('template-about.php')) {echo '<h1>关于我们</h1>';
} else {the_title();
}
?>
原文
http://www.dulizhan.bj.cn/jianzhan/204.html