douhan8610 2019-05-16 08:24
浏览 63
已采纳

为什么wp_title过滤器根本不起作用?

I know there are other questions like this but didn't find a reliable answer. So:

First activate the thing (simplyfied code):

add_action( 'after_setup_theme', 'theme_setup' );
function theme_setup() {
  add_theme_support('title-tag');
}

Second, delete title tag from header.php.

Third, on page templates, before calling get_header(), add something like this:

  add_filter('wp_title', 'set_custom_title', 10, 3);
  function set_custom_title($title, $sep, $seplocation){
    return 'test';
  }

Well, this is not working at all, in any template, being a page, an archive, a custom taxonomy or post type archive. No nothing. Wordpress is generating titles by itself.

Why? Am I doing something wrong? Note that this code once upon a time just worked: used in other sites/themes.

Is it maybe an issue of wp5.2.0?

  • 写回答

2条回答 默认 最新

  • douzhushen_9776 2019-05-16 08:53
    关注

    So, thanks to @Vel, the answer is to re-add the title tag (even if in previous wp versions > don't know til what version you had to delete it form head instead).

    Current working code for me:

    //functions.php
    add_action( 'after_setup_theme', 'theme_setup' );
    function theme_setup() {
      add_theme_support('title-tag');
    }
    
    //header.php
    <title><?php wp_title('|', true, 'right'); ?> | <?php echo get_bloginfo('name') ?></title>
    
    //page templates
    $window_title = // do something
    add_filter('wp_title', function($title, $sep, $seplocation) use($window_title){ return $window_title; }, 10, 3);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效