Theme support title tag - how to replace the default WP separator (with a filter)?

I'm developing a theme for Themeforest and one of their requirements is about thetag. Themeforest states their re

I'm developing a theme for Themeforest and one of their requirements is about the tag. Themeforest states their requirement as follows:

The theme must let WordPress add and manage the title. This is done by adding add_theme_support( 'title-tag' ); to functions.php instead of using wp_title() in the document head.

The current output is on the home page: 'Site title - Site tagline'. On other pages it is 'Post or page title - Site tagline'.

The separator is '-'. Can anyone give an example of a filter for this, so that I can change the separator to '|' or a character of my choice?

Many thanks in advance!

I'm developing a theme for Themeforest and one of their requirements is about the tag. Themeforest states their requirement as follows:

The theme must let WordPress add and manage the title. This is done by adding add_theme_support( 'title-tag' ); to functions.php instead of using wp_title() in the document head.

The current output is on the home page: 'Site title - Site tagline'. On other pages it is 'Post or page title - Site tagline'.

The separator is '-'. Can anyone give an example of a filter for this, so that I can change the separator to '|' or a character of my choice?

Many thanks in advance!

Share Improve this question edited Jul 6, 2020 at 13:02 ralphjsmit asked Jul 6, 2020 at 11:55 ralphjsmitralphjsmit 4026 silver badges23 bronze badges 9
  • The title tag shouldn't be determined by the theme. Remove the <title> tag entirely. WordPress will output it at <?php wp_head(); ?>. – Jacob Peattie Commented Jul 6, 2020 at 12:00
  • Ok @JacobPeattie thanks. I removed it and currently it displays only the website name. How should I modify that? – ralphjsmit Commented Jul 6, 2020 at 12:01
  • On every page? Make sure to remove the entire <title> tag, not just the wp_title() part. So bloginfo() should be removed to, as well as the HTML. – Jacob Peattie Commented Jul 6, 2020 at 12:10
  • Yes, I removed everything. There is now nothing in my header.php file. The only thing I have is the theme support for title tag in functions.php. Yeah, I want to modify it on every page, so [PAGE NAME OR POST NAME OR CATEGORY NAME OR WHATEVER] [SEPERATOR ( "|" )] [BLOGINFO NAME]. – ralphjsmit Commented Jul 6, 2020 at 12:11
  • It looks like title-tag uses wp_get_document_title() which, at first glance, does append the site title if you're not on the home page? There are filters there to override the separator and more too. – Rup Commented Jul 6, 2020 at 12:48
 |  Show 4 more comments

1 Answer 1

Reset to default 2

You use the document_title_separator filter.

So in your case:

<?php
function theme_prefix_filter_document_title_separator() {
    return '|';
}
add_filter( 'document_title_separator', 'theme_prefix_filter_document_title_separator' );
?>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742288387a4415736.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信