Title: get_template_partに各ページ任意のカスタムフィールドの値
Last modified: 2014年1月9日

---

# get_template_partに各ページ任意のカスタムフィールドの値

 *  解決済 [H9](https://ja.wordpress.org/support/users/harle9uin/)
 * (@harle9uin)
 * [12年、 10ヶ月前](https://ja.wordpress.org/support/topic/get_template_part%e3%81%ab%e5%90%84%e3%83%9a%e3%83%bc%e3%82%b8%e4%bb%bb%e6%84%8f%e3%81%ae%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e3%83%95%e3%82%a3%e3%83%bc%e3%83%ab%e3%83%89%e3%81%ae%e5%80%a4/)
 * 現在、Advanced Custom Fieldsを使用して、固定ページを作成しています。
    phpは以前
   wordpressのカスタマイズに少しカジってみたのですが、ほぼ、素人といった状態です。
 * 仮にcustom_fieldというフィールドを作成して、各ページ毎に任意の値をget_template_part
   に出力し、固定ページ毎にテンプレートを切り替えたいと思っています。
 * 完全にうろ覚えかつ根本がわかっていないのですが、下のようにしてみました。
    結果、
   全然ダメでした…。
 *     ```
       $cf = (array)post_custom('custom_field');
       get_template_part( 'page', '$cf' );
       ```
   
 * お分かりななられる方がいれば、お助けいただけると大変助かります。
 * よろしくお願いたします。

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

 *  トピック投稿者 [H9](https://ja.wordpress.org/support/users/harle9uin/)
 * (@harle9uin)
 * [12年、 10ヶ月前](https://ja.wordpress.org/support/topic/get_template_part%e3%81%ab%e5%90%84%e3%83%9a%e3%83%bc%e3%82%b8%e4%bb%bb%e6%84%8f%e3%81%ae%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e3%83%95%e3%82%a3%e3%83%bc%e3%83%ab%e3%83%89%e3%81%ae%e5%80%a4/#post-170785)
 * 以下で、カスタムフィールドの値は出力することは出来ました。
 *     ```
       $cf = the_field('custom_field', $post->ID);
       echo $cf;
       ```
   
 * しかし、以下のようだとエラーになってしまい、ページが表示すらされませんでした。
 *     ```
       $cf = the_field('cf', $post->ID);
       get_template_part( 'page', $cf );
       ```
   
 * フィールドcfには、test入力済み。
    page-test.phpというファイルはあがってます。（
   get_template_part( ‘page’, ‘test’ )とすると、表示されます。）
 * よろしくお願いします。
 *  [tmatsuur](https://ja.wordpress.org/support/users/tmatsuur/)
 * (@tmatsuur)
 * [12年、 10ヶ月前](https://ja.wordpress.org/support/topic/get_template_part%e3%81%ab%e5%90%84%e3%83%9a%e3%83%bc%e3%82%b8%e4%bb%bb%e6%84%8f%e3%81%ae%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e3%83%95%e3%82%a3%e3%83%bc%e3%83%ab%e3%83%89%e3%81%ae%e5%80%a4/#post-170786)
 * カトさん
    こんにちは。
 * 上記のthe_fieldはカスタムフィールドの値を取得するのではなく、値を表示するもの
   だと思います。
    $postが参照できるようになっているなら、以下でいけると思います。
 *     ```
       $cf = get_post_meta( $post->ID, 'cf', true );
       get_template_part( 'page', $cf );
       ```
   
 *  トピック投稿者 [H9](https://ja.wordpress.org/support/users/harle9uin/)
 * (@harle9uin)
 * [12年、 10ヶ月前](https://ja.wordpress.org/support/topic/get_template_part%e3%81%ab%e5%90%84%e3%83%9a%e3%83%bc%e3%82%b8%e4%bb%bb%e6%84%8f%e3%81%ae%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e3%83%95%e3%82%a3%e3%83%bc%e3%83%ab%e3%83%89%e3%81%ae%e5%80%a4/#post-170789)
 * tmatsuurさん
 * ありがとうございます。
    バッチリ表示されました!!!!
 * やっぱり、phpなどもしっかりと勉強しないとダメですね。
 * ホント助かりました。
    ありがとうございました。

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

 トピック「get_template_partに各ページ任意のカスタムフィールドの値」には新たに返信
することはできません。

 * フォーラム: [使い方全般](https://ja.wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3件の返信
 * 2人の参加者
 * 最後の返信者: [H9](https://ja.wordpress.org/support/users/harle9uin/)
 * 最後の活動: [12年、 10ヶ月前](https://ja.wordpress.org/support/topic/get_template_part%e3%81%ab%e5%90%84%e3%83%9a%e3%83%bc%e3%82%b8%e4%bb%bb%e6%84%8f%e3%81%ae%e3%82%ab%e3%82%b9%e3%82%bf%e3%83%a0%e3%83%95%e3%82%a3%e3%83%bc%e3%83%ab%e3%83%89%e3%81%ae%e5%80%a4/#post-170789)
 * 状態: 解決済

## トピック

### 人気のトピック

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

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

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

### 未解決のトピック

### すべてのトピック
