如何设置自定义页面模板的自定义标题?
我尝试使用wp_title
过滤器,但它不工作。
如何设置自定义页面模板的自定义标题?
我尝试使用wp_title
过滤器,但它不工作。
your-page-template.php
之前的文件get_header()
功能:function my_page_title() {
return \'Your value is \'; // add dynamic content to this title (if needed)
}
add_action( \'pre_get_document_title\', \'my_page_title\' );
您可以执行条件设置并检查是否在自定义帖子类型页面上,然后更新标题:
function my_wp_title( $title, $sep ) {
if (is_single(\'post-tyle\')){
// Update the title here, for example : $title .= $title . $post->post_title;
}
return $title;
}
add_filter( \'wp_title\', \'my_wp_title\', 10, 2 );
但是,您的主题必须支持wp_title
. 为此,请将此添加到主题中functions.php
文件:add_theme_support( \'title-tag\' );
有时您无法从函数执行此操作。php(您还没有想要输出的数据)。对于这些情况:
创建新的复制头文件header-mycustom.php
并将其加载到模板中
global $newtitle;
$newtitle="New Title";
get_header("mycustom");
在header-mycustom.php
代替wp_head
具有以下功能:<?php
ob_start();
wp_head();
$output = ob_get_contents();
ob_end_clean();
global $newtitle;
$output=preg_replace("/<title>(.+)<\\/title>/","<title>$newtitle</title>",$output);
echo $output;
?>
(这是我第一次觉得我必须破解WP来完成这样的普通任务。)get\\u page\\u by\\u title在纯文本标题上运行良好。但当涉及到智能引号和符号时,如# 它不起作用。它找不到具有给定标题的帖子。例如:-get_page_by_title(\'The #1 Reason to Buy Right Now – THE MONEY!!\', OBJECT, \'post\'); 退货NULL.但有一篇文章的标题是这样的。如果有更好的方法,那就太好了。