admin管理员组文章数量:1122832
I have this lovely bit of code that is running in a plugin, and generates a page using a shortcode.
...
if(count($bb_job)>0){
?>
<script type="text/javascript">
let job_title = '<?php echo $bb_job -> job_title ?>';
if(job_title) {
console.log('job title: ' + job_title)
// Change page title with job title
document.title = `Job Board: ${job_title} - Page Title`
}
</script>
<div class="bbjob">
....
The code is basically just changing the document title using the available PHP variable 'bb_job'
What I would like to do is to change the actual HTML document title properly at the Wordpress level using something like this:
<?php
function custom_document_title( $title_parts, $new_title ) {
$title_parts['title'] = $new_title;
return $title_parts;
}
add_filter( 'document_title_parts', 'custom_document_title', 1000, 2 );
But when I try any of the available filters, nothing happens. I suspect I am trying to run the filter too late?
I only want to change this one single page title, so it reflects the content of the page when it is generated.
The page itself is a single wordpress page, with dynamic content.
I guess I could put a site wide filter in the theme functions.php file, and check if I am on the 'Job Board' page before changing the title, but then how can I pass in the new Job Title as a parameter?
I have this lovely bit of code that is running in a plugin, and generates a page using a shortcode.
...
if(count($bb_job)>0){
?>
<script type="text/javascript">
let job_title = '<?php echo $bb_job -> job_title ?>';
if(job_title) {
console.log('job title: ' + job_title)
// Change page title with job title
document.title = `Job Board: ${job_title} - Page Title`
}
</script>
<div class="bbjob">
....
The code is basically just changing the document title using the available PHP variable 'bb_job'
What I would like to do is to change the actual HTML document title properly at the Wordpress level using something like this:
<?php
function custom_document_title( $title_parts, $new_title ) {
$title_parts['title'] = $new_title;
return $title_parts;
}
add_filter( 'document_title_parts', 'custom_document_title', 1000, 2 );
But when I try any of the available filters, nothing happens. I suspect I am trying to run the filter too late?
I only want to change this one single page title, so it reflects the content of the page when it is generated.
The page itself is a single wordpress page, with dynamic content.
I guess I could put a site wide filter in the theme functions.php file, and check if I am on the 'Job Board' page before changing the title, but then how can I pass in the new Job Title as a parameter?
Share Improve this question asked Sep 27, 2024 at 1:29 atlas_scoffedatlas_scoffed 1011 bronze badge 01 Answer
Reset to default 1It seems that the way you attached your function to the document_title_parts
filter is wrong. As per WordPress documentation, document_title_parts
only accepts one parameters, array $title
. So it doesn't make sense that when you have 2 parameters in your function.
I suggest you do something like this:
function custom_document_title( $title_parts ) {
// Here is where you need to get the $bb_job variable
$bb_job = null; // Change this to the way you use to obtain $bb_job
// If $bb_job is not empty, assign $bb_job -> job_title
if($bb_job && $bb_job->job_title){
$title_parts['title'] = $bb_job->job_title;
}
return $title_parts;
}
add_filter( 'document_title_parts', 'custom_document_title', 1000, 1 );
本文标签: Changing a page title while the page is being built
版权声明:本文标题:Changing a page title while the page is being built 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736287592a1927914.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论