Title: add_filterでthe_titleをpost_type毎に出力したい
Last modified: 2020年10月6日

---

# add_filterでthe_titleをpost_type毎に出力したい

 *  解決済 [lotokunn](https://ja.wordpress.org/support/users/lotokunn/)
 * (@lotokunn)
 * [6年、 1ヶ月前](https://ja.wordpress.org/support/topic/add_filter%e3%81%a7the_title%e3%82%92post_type%e6%af%8e%e3%81%ab%e5%87%ba%e5%8a%9b%e3%81%97%e3%81%9f%e3%81%84/)
 *     ```
       <?php
   
       // タイトルをACFの製品名に変更
       function my_auto_title( $title ){
   
           global $post;
   
           // ポストタイプがカスタムかどうかチェック
           if( $post->post_type == 'product' ){
               $title = get_field( 'product_name' );
           }
   
           return $title;
   
       }
   
           add_filter( 'the_title', 'my_auto_title', '未設定' );
           add_filter( 'single_post_title', 'my_auto_title', '未設定' );
   
       ?>
       ```
   
 * 上記の様にadd_filterを使いmy_autotitleを読み込ませています。
    $titleは、ACField
   のプラグインを使いカスタムフィールドのラベル名を取得しています。
 * home.php画面では正常に、メニューの表紙がされます。（ダッシュボードのメニューから
   作ったメニュー）
    また、home中のthe_title( ”, ‘ ‘, true )もACFieldのラベルが正常
   に表示されます。
 * しかし、
    single.php中のメニューがACFieldのラベル名に変わってしまいます。
 * 原因が分からないのですが、感覚として、single.phpに遷移した際にそのpost_typeを
   取得して(post_type = product)、the_title()が変わっている様に感じます。
 * どの様にすれば、
    single.php中のメニューはダッシュボードから作ったメニューの表示
   にようのままにして、 the_title()を、ACFieldのカスタムフィールドのラベル名にできる
   のでしょうか？？？
 * どなたかご教授、いただきたく思います。

2件の返信を表示中 - 1 - 2件目 (全2件中)

 *  [ishitaka](https://ja.wordpress.org/support/users/ishitaka/)
 * (@ishitaka)
 * [6年、 1ヶ月前](https://ja.wordpress.org/support/topic/add_filter%e3%81%a7the_title%e3%82%92post_type%e6%af%8e%e3%81%ab%e5%87%ba%e5%8a%9b%e3%81%97%e3%81%9f%e3%81%84/#post-11667118)
 * こんにちは
 * グローバル変数の $post は、現在のページの投稿オブジェクトになります。
    the_title
   フックの第2引数の投稿 ID、single_post_title フックの第2引数の投稿オブジェクト
   を使用するといいのではないでしょうか？
 * 例:
 *     ```
       function my_auto_title( $title, $p ){
   
       	//global $post;
       	$p = is_object( $p ) ? $p : get_post( $p );
   
       	// ポストタイプがカスタムかどうかチェック
       	if ( $p->post_type == 'product' ) {
       		$title = get_field( 'product_name', $p->ID );
       	}
   
           return $title;
   
       }
   
       add_filter( 'the_title', 'my_auto_title', 10, 2 );
       add_filter( 'single_post_title', 'my_auto_title', 10, 2 );
       ```
   
 *  トピック投稿者 [lotokunn](https://ja.wordpress.org/support/users/lotokunn/)
 * (@lotokunn)
 * [6年、 1ヶ月前](https://ja.wordpress.org/support/topic/add_filter%e3%81%a7the_title%e3%82%92post_type%e6%af%8e%e3%81%ab%e5%87%ba%e5%8a%9b%e3%81%97%e3%81%9f%e3%81%84/#post-11667119)
 * 今晩は。ishitake様。早速、返信ありがとうございます。
 * 無事、問題がクリア出来ました。ありがとうございます。
 * global $postの使い方が違った様ですね。理解しました。
    今後とも、よろしくお願い
   いたします。

2件の返信を表示中 - 1 - 2件目 (全2件中)

 トピック「add_filterでthe_titleをpost_type毎に出力したい」には新たに返信すること
はできません。

## タグ

 * [ACF](https://ja.wordpress.org/support/topic-tag/acf/)
 * [add_filter](https://ja.wordpress.org/support/topic-tag/add_filter/)
 * [the_title()](https://ja.wordpress.org/support/topic-tag/the_title/)

 * フォーラム: [使い方全般](https://ja.wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2件の返信
 * 2人の参加者
 * 最後の返信者: [lotokunn](https://ja.wordpress.org/support/users/lotokunn/)
 * 最後の活動: [6年、 1ヶ月前](https://ja.wordpress.org/support/topic/add_filter%e3%81%a7the_title%e3%82%92post_type%e6%af%8e%e3%81%ab%e5%87%ba%e5%8a%9b%e3%81%97%e3%81%9f%e3%81%84/#post-11667119)
 * 状態: 解決済

## トピック

### 人気のトピック

### 返信がないトピック

### サポート関連ではないトピック

### 解決済みのトピック

### 未解決のトピック

### すべてのトピック
