Skip to content

Instantly share code, notes, and snippets.

@mypacecreator
Last active December 15, 2021 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mypacecreator/5dcbed010b9fa196086d to your computer and use it in GitHub Desktop.
Save mypacecreator/5dcbed010b9fa196086d to your computer and use it in GitHub Desktop.
WordPress4.1以降でカスタムフィールドを使い、投稿ごとにタイトルタグを自由に編集するフィルターフックその1
<?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