WordPress4.4でカスタムフィールドを使い、投稿ごとにタイトルタグを自由に編集するフィルターフックその2(サイト名はそのまま使う)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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