Skip to content

Instantly share code, notes, and snippets.

@mypacecreator
Last active December 15, 2021 08:01
Embed
What would you like to do?
WordPress4.4でカスタムフィールドを使い、投稿ごとにタイトルタグを自由に編集するフィルターフックその2(サイト名はそのまま使う)
<?php
//コピペするなら3行目から↓
function mypace_custom_title( $title ){
if( is_singular() ){ //タイトルタグカスタマイズの範囲を条件分岐で指定
$post_id = get_the_ID(); //投稿IDを取得
$my_title = get_post_meta( $post_id, 'my_title', true ); //カスタムフィールドの値を取得
if( $my_title ){ //カスタムフィールドに値がある時
$title['title'] = esc_html( $my_title ); //ページタイトルの部分のみ上書き
return $title;
}
}
return $title; //条件外の時はWordPressコアで定義されているタイトルのまま出力
}
add_filter( 'document_title_parts', 'mypace_custom_title' ); //フィルターフックで処理を上書き
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment