add_filter('wp_title') 没有替换我的标题标签(WordPress 插件)
add_filter('wp_title') doesn't replace my title tag (WordPress plugin)
我正在尝试将我的详细信息页面的标题更改为汽车名称。
我做了一个插件,用汽车信息填充详细信息页面。但是现在我无法让 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() 函数运行良好。它 returns 正确的名称。 add_action('wp_head') 也可以。我只是无法让 wp_title 过滤器不起作用。
我是不是在错误的时刻执行了这个过滤器,或者我做错了什么?
我无法从您提供的代码中看出,但您使用的是:
<title><?php wp_title(); ?></title>
在您的 <head>
下,在 header.php?
下
更新
显然,从 4.4 开始,标题的处理方式发生了变化。这里有一个 link 解释了如何使用新代码:
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;
}
如果其他人遇到此问题,可能是因为 Yoast 插件。使用:
add_filter( 'pre_get_document_title', function( $title ){
// Make any changes here
return $title;
}, 999, 1 );
我尝试了 100 多个示例,遍历了网络上的每个解决方案。
最后...干得好 joemaller因为把这个放在第一位就解决了所有问题!
add_filter('wpseo_title', '__return_empty_string');
我将此答案发布到另一个问题,但由于它是相关的并且更多 up-to-date,我认为它可能有用。
自 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
过滤器,它 short-circuits 生成标题,因此如果您不打算对当前标题进行更改,则性能更高:
function custom_document_title( $title ) {
return 'Here is the new title';
}
add_filter( 'pre_get_document_title', 'custom_document_title', 10 );
第二种方法使用 document_title_separator
和 document_title_parts
挂钩标题以及稍后在函数中执行的标题分隔符,在使用 single_term_title
或 [= 等函数生成标题之后19=] 取决于页面即将输出:
// 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 );
我正在尝试将我的详细信息页面的标题更改为汽车名称。 我做了一个插件,用汽车信息填充详细信息页面。但是现在我无法让 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() 函数运行良好。它 returns 正确的名称。 add_action('wp_head') 也可以。我只是无法让 wp_title 过滤器不起作用。
我是不是在错误的时刻执行了这个过滤器,或者我做错了什么?
我无法从您提供的代码中看出,但您使用的是:
<title><?php wp_title(); ?></title>
在您的 <head>
下,在 header.php?
更新
显然,从 4.4 开始,标题的处理方式发生了变化。这里有一个 link 解释了如何使用新代码:
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;
}
如果其他人遇到此问题,可能是因为 Yoast 插件。使用:
add_filter( 'pre_get_document_title', function( $title ){
// Make any changes here
return $title;
}, 999, 1 );
我尝试了 100 多个示例,遍历了网络上的每个解决方案。
最后...干得好 joemaller因为把这个放在第一位就解决了所有问题!
add_filter('wpseo_title', '__return_empty_string');
我将此答案发布到另一个问题,但由于它是相关的并且更多 up-to-date,我认为它可能有用。
自 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
过滤器,它 short-circuits 生成标题,因此如果您不打算对当前标题进行更改,则性能更高:
function custom_document_title( $title ) {
return 'Here is the new title';
}
add_filter( 'pre_get_document_title', 'custom_document_title', 10 );
第二种方法使用 document_title_separator
和 document_title_parts
挂钩标题以及稍后在函数中执行的标题分隔符,在使用 single_term_title
或 [= 等函数生成标题之后19=] 取决于页面即将输出:
// 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 );