最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get string from add_theme_support('title-tag') for meta tag and beautify?

programmeradmin1浏览0评论

It recommends that we should use add_theme_support('title-tag') for displaying blog title. However, I also need to get the title as string for another purpose. It's for optimizing SEO. As you already know, some service like OG or Twitter has their own title meta tag like:

<meta property='og:title' content='need to be filled in'/>
<meta property='twitter:title' content='need to be filled in'/>

And the problem is I don't have a good way to query a title string to fill in the content. Also, another problem is I want to customize title format, like setting blog's name position or using a different separator.

So, do you guy have anyway to query a title string that can:

  • be used to add to meta tags?
  • be modified its format?

It recommends that we should use add_theme_support('title-tag') for displaying blog title. However, I also need to get the title as string for another purpose. It's for optimizing SEO. As you already know, some service like OG or Twitter has their own title meta tag like:

<meta property='og:title' content='need to be filled in'/>
<meta property='twitter:title' content='need to be filled in'/>

And the problem is I don't have a good way to query a title string to fill in the content. Also, another problem is I want to customize title format, like setting blog's name position or using a different separator.

So, do you guy have anyway to query a title string that can:

  • be used to add to meta tags?
  • be modified its format?
Share Improve this question edited Sep 24, 2019 at 7:54 Star Light asked Sep 22, 2019 at 8:03 Star LightStar Light 518 bronze badges 3
  • See wp_get_document_title() which you can use to get the document title, and for changing the title parts, see document_title_separator and document_title_parts. – Sally CJ Commented Sep 24, 2019 at 0:55
  • I see, I can just replace the result of wp_get_document_title() with the pattern ' - ' right. Btw, could you post the answer so I could close the question please? – Star Light Commented Sep 24, 2019 at 8:33
  • Sorry for the late reply. And yes, basically. But if you're trying to change the document title separator (which defaults to -/hypen), then you should use the document_title_separator hook as in my answer. – Sally CJ Commented Sep 24, 2019 at 16:53
Add a comment  | 

1 Answer 1

Reset to default 1

I don't have a good way to query a title string to fill in the content

You can use wp_get_document_title() to get the document title.

So for example in your case: (see the docs for details on esc_attr())

<?php
// Put the title (string) in a variable.
$title_esc = esc_attr( wp_get_document_title() );
?>
<meta property='og:title' content='<?php echo $title_esc; ?>'/>
<meta property='twitter:title' content='<?php echo $title_esc; ?>'/>

I want to customize title format, like setting blog's name position

You can do that via the document_title_parts hook.

But first, the document title has the following parts by default, and they're passed as an array to callbacks hooked to document_title_parts:

| Part                                | Array key | Optional | Position |
|-------------------------------------|-----------|----------|----------|
| Title of the viewed page.           | title     | No       | 1        |
| Page number if paginated.           | page      | Yes      | 2        |
| Site description when on home page. | tagline   | Yes      | 3        |
| Site title when not on home page.   | site      | Yes      | 4        |

The "optional" state means that the array item may or may not exist on certain pages — e.g. on page 1 of a category archive, the page item doesn't exist (or is excluded) by default, but on page 2 and above, page is included.

So for example, if you want the blog/site title to be the first instead of the default last:

function my_document_title_parts( $title ) {
    if ( isset( $title['site'] ) ) {
        $site = $title['site'];  // get
        unset( $title['site'] ); // unset
        // Then put it at the top.
        $title = array_merge( [ 'site' => $site ], $title );
    }
    // Remember to return an array.
    return $title;
}
add_filter( 'document_title_parts', 'my_document_title_parts' );

And based on the above code, here are some sample output of var_dump( $title ):

// Home page.
array (
  'title' => 'My Site',
  'tagline' => 'Just another cool WordPress site',
)

// Category page 2.
array (
  'title' => 'Uncategorized',
  'page' => 'Page 2',
  'site' => 'My Site',
)

// Single Post.
array (
  'title' => 'Markup: HTML Tags and Formatting',
  'site' => 'My Site',
)

or using a different separator

You can do that via the document_title_separator hook.

Here's an example for using | than the default - (i.e. a hypen):

function my_document_title_separator( $sep ) {
    return '|';
}
add_filter( 'document_title_separator', 'my_document_title_separator' );

Notes

  • Changing the title parts via document_title_parts or the title separator via document_title_separator will also change the title parts/separator which WordPress use in the title tag. There are solutions such as using a closure, but I'm not giving any examples for that (for now).
发布评论

评论列表(0)

  1. 暂无评论