Skip to content

Instantly share code, notes, and snippets.

@mypacecreator
Last active December 15, 2021 08:01
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/872a3458160aef786089 to your computer and use it in GitHub Desktop.
Save mypacecreator/872a3458160aef786089 to your computer and use it in GitHub Desktop.
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