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

报告相同问题?

悬赏问题

  • ¥60 不懂得怎么运行下载来的代码
  • ¥15 CST导出3D模型图为什么和软件显示不一样?
  • ¥15 加热反应炉PLC控制系统设计(相关搜索:梯形图)
  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 经过滑动平均后的一维信号还原用什么结构好呢?
  • ¥15 指定IP电脑的访问设置
  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 为什么devc++编译项目会失败啊
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒