我在Wordpress网站上添加了一个自定义重写规则,如下所示:
add_rewrite_rule( 'register/bronze', 'index.php?action=register&type=2', 'top' );
然后在template_redirect
操作中,我检查action
查询字符串变量并加载我的include,如下所示:
$action = get_query_var( 'action' );
if( $action == "register" ) {
include( BG_FILE_PATH . '/templates/register-brand.php' );
exit;
}
这一切都很好,我的自定义模板会显示出来,但页面标题显示为"找不到页面|网站名称"。
有没有一种方法可以从我的自定义模板中设置页面标题?我尽量避免将这些页面设置为Wordpress页面,因为它们是网站运行的基础,我不希望管理员更改页面设置或完全删除页面。
非常感谢您的帮助。
WordPress很可能会覆盖您的标题,因为它仍然抛出404(您可以使用firebug验证这一点)。
WordPress通常会在模板重定向中包含文件时抛出404。
您需要使用以下代码重置标题:
global $wp_query;
$wp_query->is_404 = false;
status_header( '200' ); //if status_header doesn't work, try header("HTTP/1.1 200 OK")
//now that you reset the 404, you can proceed with your include
$action = get_query_var( 'action' );
if( $action == "register" ) {
include( BG_FILE_PATH . '/templates/register-brand.php' );
exit;
}
这将重置状态标头,并允许包含文件的标题正常显示。不需要javascript:)
您可以通过插入一小段JavaScript:来实现这一点
<script>
$(document).ready(function() {
document.title = 'your title here';
});
</script>
如果你希望标题是动态的,那么替换为:
document.title = <?php echo 'your dynamic title here'; ?>;