add_filter('wp_title ')不会替换我的标题标签(WordPress插件)

fruv7luv  于 2022-12-22  发布在  WordPress
关注(0)|答案(5)|浏览(147)

我正在尝试将详细信息页面的标题更改为汽车的名称。我制作了一个插件,可以用汽车信息填充详细信息页面。但是现在我无法让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过滤器不工作。
我是否在错误的时间执行此筛选器,或者我做错了什么?

w1jd8yoj

w1jd8yoj1#

如果其他人对此有问题,可能是由于Yoast插件。用途:

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

k10s72fa2#

我不能告诉从你提供的代码,但你使用:

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

在你的<head>里,在. 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; 
}
wf82jlnq

wf82jlnq3#

我贴出了另一个问题的答案,但由于它是相关的,更及时,我认为它可能是有用的。
自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中的代码。以下是可用于操作title标记的过滤器:

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钩子来处理title和title分隔符,它们在函数中稍后执行,在title根据页面使用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 );
9wbgstp7

9wbgstp74#

我已经在网上搜索了每一个解决方案,尝试了100多个例子。
最后...做得好joemaller因为把这个放在第一位解决了一切!

add_filter('wpseo_title', '__return_empty_string');
1l5u6lss

1l5u6lss5#

    • 简单的答案是使用此变量。**
document_title_parts();
    • 例如:**
add_filter( 'document_title_parts', function( $title ){
  // Customize here
  return clean( $title );
}, 10 );
    • 在此之前输出:**
#Page Title
    • 输出后:**
Page Title

谢谢。

相关问题