【问题标题】:add_filter('wp_title') doesn't replace my title tag (WordPress plugin)add_filter('wp_title') 不会替换我的标题标签(WordPress 插件)
【发布时间】:2016-07-05 09:14:57
【问题描述】:

我正在尝试将我的详细信息页面的标题更改为汽车的名称。 我制作了一个用汽车信息填充详细信息页面的插件。但现在我无法让 add_filter('wp_title') 在我的插件中工作。

这是我试过的代码:

function init() {
    hooks();
}
add_action('wp', 'init');

function hooks() {
    if(get_query_var('car_id') != "") {
        add_filter('wp_title', 'addTitle', 100);
        add_action('wp_head', 'fillHead');
    }
    add_shortcode('showCar', 'showCar');
}

function addTitle() {
    $api_url_klant = API_URL . '/gettitle/' . get_option("ac") . '/' . get_query_var('car_id');
    $title = getJSON($api_url_klant);
    return $title['merk'] . " " . $title['model'] . " - " . $title['bedrijf'];
}

addTitle() 函数工作得很好。它返回正确的名称。 add_action('wp_head') 也有效。我只是无法让 wp_title 过滤器不起作用。

我是在错误的时刻执行了这个过滤器还是我做错了什么?

【问题讨论】:

    标签: wordpress plugins title


    【解决方案1】:

    我无法从您提供的代码中看出,但您是否在使用:

    <title><?php wp_title(); ?></title>
    

    在您的&lt;head&gt; 中,在 header.php 下?

    更新

    显然,从 4.4 开始,对标题的处理方式进行了更改。这是一个解释如何使用新代码的链接:

    https://www.developersq.com/change-page-post-title-wordpress-4-4/

    /*
     * Override default post/page title - example
     * @param array $title {
     *     The document title parts.
     *
     *     @type string $title   Title of the viewed page.
     *     @type string $page    Optional. Page number if paginated.
     *     @type string $tagline Optional. Site description when on home page.
     *     @type string $site    Optional. Site title when not on home page.
     * }
     *     @since WordPress 4.4
     *     @website: www.developersq.com
     *     @author: Aakash Dodiya
    */
    add_filter('document_title_parts', 'dq_override_post_title', 10);
    function dq_override_post_title($title){
       // change title for singular blog post
        if( is_singular( 'post' ) ){ 
            // change title parts here
            $title['title'] = 'EXAMPLE'; 
        $title['page'] = '2'; // optional
        $title['tagline'] = 'Home Of Genesis Themes'; // optional
            $title['site'] = 'DevelopersQ'; //optional
        }
    
        return $title; 
    }
    

    【讨论】:

    • 我想是的。这就是标准 wordpress 主题中的内容吗?它在一个插件中,所以每种标题都应该被替换
    • 似乎是二十四,但我看不到二十五或二十六。我从不使用默认的 WP 主题,所以我不确定……但我已经使用了与你的代码类似的代码数十次。我总是确保 wp_title() 是手动调用的......添加它,看看情况是否改变。
    • 它应该在插件中工作,因为它将在许多站点上使用。所以我不想改变任何主题
    • 更新了我的答案以反映新的 WordPress 标题方法。
    • 对于尝试更改此设置后到达这里的任何其他人,请注意标语(网站描述)默认情况下放在标题中。通过使用这个答案并设置$title['tagline'] = get_bloginfo('description');,我很容易得到它。
    【解决方案2】:

    如果其他人对此有疑问,可能是因为 Yoast 插件。使用:

    add_filter( 'pre_get_document_title', function( $title ){
        // Make any changes here
        return $title;
    }, 999, 1 );
    

    【讨论】:

    • 为了绕过 Yoast 的标题过滤器但保留 document_title_parts 的好处,我们还可以在 wpseo_title 过滤器中返回一个空字符串:add_filter('wpseo_title', '__return_empty_string'); 这似乎让 Yoast 回退到原生WordPress 标题。
    【解决方案3】:

    我已经尝试了 100 多个示例,浏览了网络上的每一个解决方案。

    最后...干得好joemaller首先解决了所有问题!

    add_filter('wpseo_title', '__return_empty_string');
    

    【讨论】:

      【解决方案4】:

      我将这个答案发布到另一个问题,但由于它是相关且更新的,我认为它可能有用。

      自 Wordpress v4.4.0 以来,文档标题的生成方式发生了变化。现在wp_get_document_title 指示标题的生成方式:

      /**
       * Displays title tag with content.
       *
       * @ignore
       * @since 4.1.0
       * @since 4.4.0 Improved title output replaced `wp_title()`.
       * @access private
       */
      function _wp_render_title_tag() {
          if ( ! current_theme_supports( 'title-tag' ) ) {
              return;
          }
      
          echo '<title>' . wp_get_document_title() . '</title>' . "\n";
      }
      

      这是来自 v5.4.2 的代码。以下是可用于操作标题标签的过滤器:

      function wp_get_document_title() {
          /**
          * Filters the document title before it is generated.
          *
          * Passing a non-empty value will short-circuit wp_get_document_title(),
          * returning that value instead.
          *
          * @since 4.4.0
          *
          * @param string $title The document title. Default empty string.
          */
          $title = apply_filters( 'pre_get_document_title', '' );
          if ( ! empty( $title ) ) {
              return $title;
          }
          // --- snipped ---
          /**
          * Filters the separator for the document title.
          *
          * @since 4.4.0
          *
          * @param string $sep Document title separator. Default '-'.
          */
          $sep = apply_filters( 'document_title_separator', '-' );
      
          /**
          * Filters the parts of the document title.
          *
          * @since 4.4.0
          *
          * @param array $title {
          *     The document title parts.
          *
          *     @type string $title   Title of the viewed page.
          *     @type string $page    Optional. Page number if paginated.
          *     @type string $tagline Optional. Site description when on home page.
          *     @type string $site    Optional. Site title when not on home page.
          * }
          */
          $title = apply_filters( 'document_title_parts', $title );
          // --- snipped ---
          return $title;
      }
      

      所以这里有两种方法可以做到。

      第一个使用pre_get_document_title 过滤器,它会缩短标题生成,因此如果您不打算对当前标题进行更改,则性能会更高:

      function custom_document_title( $title ) {
          return 'Here is the new title';
      }
      add_filter( 'pre_get_document_title', 'custom_document_title', 10 );
      

      第二种方法使用document_title_separatordocument_title_parts 钩子作为标题和标题分隔符,在函数后面执行,在使用single_term_titlepost_type_archive_title 等函数生成标题之后,具体取决于页面和即将被输出:

      // Custom function should return a string
      function custom_seperator( $sep ) {
         return '>';
      }
      add_filter( 'document_title_separator', 'custom_seperator', 10 );
      
      // Custom function should return an array
      function custom_html_title( $title ) {
         return array(
           'title' => 'Custom Title',
           'site'  => 'Custom Site'
          );
      }
      add_filter( 'document_title_parts', 'custom_html_title', 10 );
      

      【讨论】:

        猜你喜欢
        • 2015-10-05
        • 2019-08-10
        • 1970-01-01
        • 1970-01-01
        • 2011-11-12
        • 1970-01-01
        • 2012-10-04
        • 2018-02-16
        • 1970-01-01
        相关资源
        最近更新 更多