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 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题
  • ¥15 fanuc机器人PRIO083数字信号未复原错误,如何解决?
  • ¥20 如何为现有电路板增加远程控制功能
  • ¥15 C#点击按钮的时候的循环次数就是最后一次,如何是循环第几次的值?
  • ¥15 UE5打包失败,求解决
  • ¥15 请问STM32G431的CANOPEN协议函数怎么写
  • ¥15 graphpad prism 三因素重复测定报错