I have a page template, used on a single page, which takes URL string parameters like this...
Facilitated like this...
// Inherit the meta target
global $wp_query;
if (isset($wp_query->query_vars['tags'])) {
$tax_meta_key = 'tags';
$tax_meta_value = $wp_query->query_vars['tags'];
}
It works.
But now I also want to dynamically alter the title of the page based on the same query input.
I have read about several title filter methods. document_title_parts
suits, since it only changes the title part and leaves the site name and formulation in tact.
However, the following does not work; it just results in a blank title part (not site-name part)...
<?php
// Inherit the meta target
global $wp_query;
if (isset($wp_query->query_vars['tags'])) {
$tax_meta_key = 'tags';
$tax_meta_value = $wp_query->query_vars['tags'];
}
// Filter to customise page title from just "Organisation Type"
function custom_title($title_parts) {
$title_parts['title'] = $tax_meta_value;
return $title_parts;
}
add_filter( 'document_title_parts', 'custom_title' );
get_header();
?>
I suspect this may be down to the order in which a query_vars
and a document_title_parts
are executed (?) - ie. Are the vars processed after get_header
?
Can I include the var in my page title?
I have a page template, used on a single page, which takes URL string parameters like this...
http://www.example/mypage?tags=Publishing
Facilitated like this...
// Inherit the meta target
global $wp_query;
if (isset($wp_query->query_vars['tags'])) {
$tax_meta_key = 'tags';
$tax_meta_value = $wp_query->query_vars['tags'];
}
It works.
But now I also want to dynamically alter the title of the page based on the same query input.
I have read about several title filter methods. document_title_parts
suits, since it only changes the title part and leaves the site name and formulation in tact.
However, the following does not work; it just results in a blank title part (not site-name part)...
<?php
// Inherit the meta target
global $wp_query;
if (isset($wp_query->query_vars['tags'])) {
$tax_meta_key = 'tags';
$tax_meta_value = $wp_query->query_vars['tags'];
}
// Filter to customise page title from just "Organisation Type"
function custom_title($title_parts) {
$title_parts['title'] = $tax_meta_value;
return $title_parts;
}
add_filter( 'document_title_parts', 'custom_title' );
get_header();
?>
I suspect this may be down to the order in which a query_vars
and a document_title_parts
are executed (?) - ie. Are the vars processed after get_header
?
Can I include the var in my page title?
If your query var tags
is registered properly then the following code snippet will definitely work, I've tested it. It's a modified version of your code snippet. Please make sure to add the code to your functions.php
file.
function prefix_custom_title($title_parts) {
global $wp_query;
if (isset($wp_query->query_vars['tags'])) {
$title_parts['title'] = $wp_query->query_vars['tags'];
}
return $title_parts;
}
add_filter( 'document_title_parts', 'prefix_custom_title' );
$tax_meta_value
is undefined from your filter callback. You can doglobal $tax_meta_value;
, or better, move theglobal $wp_query; if ...
part to the filter callback. – Sally CJ Commented Jan 27, 2019 at 9:22get_query_var()
-$title_parts['title'] = get_query_var( 'tags' )
. – Sally CJ Commented Jan 27, 2019 at 9:33