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条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog