使用PHP在WordPress中设置页面标题
发布时间:2020-12-13 13:10:55 所属栏目:PHP教程 来源:网络整理
导读:我知道这个问题之前已被问过几千次,但我尝试了很多这些解决方案却没有取得任何成功. 尝试过的解决方案等(添加到模板页面) add_filter('the_title','callback_to_set_the_title');add_filter('wp_title','callback_to_set_the_title');global $title;$title =
我知道这个问题之前已被问过几千次,但我尝试了很多这些解决方案却没有取得任何成功.
尝试过的解决方案等(添加到模板页面) add_filter('the_title','callback_to_set_the_title'); add_filter('wp_title','callback_to_set_the_title'); global $title; $title = 'My Custom Title'; add_filter( 'pre_get_document_title','callback_to_set_the_title' ); add_filter( 'document_title_parts','callback_to_set_the_title' ); 方案是我有一个工作列表页面的自定义模板.这个页面有重写的URL重写domain.com/about/careers/search/job?slug=whatever到domain.com/about/careers/search/job/whatever- slug用于从Redis检索一个条目包含工作列表的所有信息,包括我想要使用的标题. 这是我如何重写URL(来自functions.php): function job_listing_url_rewrite() { global $wp_rewrite; add_rewrite_tag('%slug%','([^&]+)'); add_rewrite_rule('^about/careers/search/job/([a-zA-Z0-9-_]+)/?','index.php?page_id=6633&slug=$matches[1]','top'); $wp_rewrite->flush_rules(true); } add_action('init','job_listing_url_rewrite',10,0);
我用我的模板进行了一些研究.我的模板调用get_header来打印带有head等的html标签,我想你的确是一样的.
要替换标题,我在调用该函数之前启动输出缓冲并在之后获取它. ob_start(); get_header(); $header = ob_get_clean(); $header = preg_replace('#<title>(.*?)</title>#','<title>TEST</title>',$header); echo $header; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |