pages - How to include a query_vars value in document_title_parts?

admin2025-06-04  2

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?

Share Improve this question asked Jan 27, 2019 at 7:28 Robert AndrewsRobert Andrews 9881 gold badge19 silver badges42 bronze badges 2
  • 1 $tax_meta_value is undefined from your filter callback. You can do global $tax_meta_value;, or better, move the global $wp_query; if ... part to the filter callback. – Sally CJ Commented Jan 27, 2019 at 9:22
  • Or in that callback, you could simply use get_query_var() - $title_parts['title'] = get_query_var( 'tags' ). – Sally CJ Commented Jan 27, 2019 at 9:33
Add a comment  | 

1 Answer 1

Reset to default 0

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' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748971390a315269.html

最新回复(0)