- サイトデザイン工事中です。ご意見をお寄せください。
- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
関数リファレンス/get post custom
提供:WordPress Codex 日本語版
< 関数リファレンス
指定した記事や固定ページから、すべてのカスタムフィールドの情報を配列で取得します。get_post_custom_keys() や get_post_custom_values() も参照してください。
使い方
<?php $custom_fields = get_post_custom($post_id); ?>
パラメータ
- $post_id
- (整数) (オプション) カスタムフィールド情報を取得したい投稿の ID
- 初期値: 現在の投稿の ID
用例
デフォルトの使い方
以下の例では $custom_fields に 現在の 投稿のすべてのカスタムフィールド情報を配列で取得します。
<?php $custom_fields = get_post_custom(); ?>
カスタムフィールドの配列からデータを取得する
以下のサンプルは、ID 72 の投稿から my_custom_field というキーを持つカスタムフィールドの値を配列ですべて取得します。ここでは、このキーを持つカスタムフィールドは3つあり、値はそれぞれ "犬", "47", "その他の値" とします。
<?php $custom_fields = get_post_custom(72); // 指定した投稿のすべてのカスタムフィールド情報を取得 $my_custom_field = $custom_fields['my_custom_field']; // 'my_custom_field' というキーを持つカスタムフィールドの値を取得 foreach ( $my_custom_field as $key => $value ) echo $key . " => " . $value . "<br />"; ?>
出力結果:
0 => 犬
1 => 47
2 => その他の値
関連資料
カスタムフィールド: the_meta, get_post_meta, add_post_meta, update_post_meta, delete_post_meta, get_post_custom, get_post_custom_values, get_post_custom_keys (post_meta 関数の例も参照)
最新英語版: WordPress Codex » Function Reference/get post custom (最新版との差分)