WordPress4.1以降でカスタムフィールドを使い、投稿ごとにタイトルタグを自由に編集するフィルターフックその1
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 ){ //カスタムフィールドに値がある時 | |
return esc_html( $my_title ); //エスケープして出力 | |
} | |
} | |
return $title; //条件外の時はWordPressコアで定義されているタイトルのまま出力 | |
} | |
add_filter( 'wp_title', 'mypace_custom_title' ); //旧フィルターフックで処理を上書き |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment