add_filter('wp_title')는 타이틀 태그(WordPress 플러그인)를 대체하지 않습니다.
제 상세 페이지 제목을 차명으로 바꾸려고 합니다.상세 페이지를 자동차 정보로 채우는 플러그인을 만들었습니다.그러나 현재 플러그인에서 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 필터만 작동하지 않습니다.
이 필터를 잘못된 타이밍에 실행하고 있는 건가요, 아니면 무엇을 잘못하고 있는 건가요?
다른 사용자가 이 문제를 겪고 있는 경우 Yoast 플러그인이 원인일 수 있습니다.용도:
add_filter( 'pre_get_document_title', function( $title ){
// Make any changes here
return $title;
}, 999, 1 );
당신이 제공한 코드로는 알 수 없지만, 당신은 다음을 사용하고 있습니까?
<title><?php wp_title(); ?></title>
당신의 안에서<head>, header.displaces 아래에 있습니까?
갱신하다
타이틀의 취급방법이 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 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_separator그리고.document_title_parts다음과 같은 함수를 사용하여 제목이 생성된 후 함수에서 나중에 실행되는 제목과 제목 구분자를 위한 후크single_term_title또는post_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 );
저는 웹상의 모든 솔루션을 탐색하여 100개의 예를 시험해 보았습니다.
결국...잘했어 조멀러 이걸 먼저 놓으니까 모든 게 해결됐어!
add_filter('wpseo_title', '__return_empty_string');
간단한 답은 이 변수를 사용하는 것입니다.
document_title_parts();
예를 들어 다음과 같습니다.
add_filter( 'document_title_parts', function( $title ){
// Customize here
return clean( $title );
}, 10 );
출력 전:
#Page Title
출력 후:
Page Title
감사해요.
언급URL : https://stackoverflow.com/questions/36087390/add-filterwp-title-doesnt-replace-my-title-tag-wordpress-plugin
'programing' 카테고리의 다른 글
| gson이 Malformed Json Exception을 슬로우하다 (0) | 2023.03.06 |
|---|---|
| 커스텀 투고 타입에 기본 카테고리/태그 분류법을 재사용하시겠습니까? (0) | 2023.02.11 |
| 리액트 라우터가 404 상태 코드로 응답하도록 하려면 어떻게 해야 합니까? (0) | 2023.02.11 |
| 도커 구성 볼륨 사용 권한 리눅스 (0) | 2023.02.11 |
| 이 경고 메시지는 무엇을 의미합니까?'img 요소에는 의미 있는 텍스트가 포함된 alt 소품이나 장식 이미지용 빈 문자열이 있어야 합니다.' (0) | 2023.02.11 |