$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>pages - How to include a query_vars value in document_title_parts?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

pages - How to include a query_vars value in document_title_parts?

matteradmin9PV0评论

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' );
Post a comment

comment list (0)

  1. No comments so far