get_post_meta() と同様に、投稿を返すために使用されるカスタム フィールドには関数が必要ですが、get_post_custom() 関数の方が使いやすく、ループ内で使用する場合はパラメータを設定する必要さえありません。
実際、get_post_custom() 関数の基本的な実装は get_post_meta()~
get_post_custom() と似ており、
get_post_custom($postid);
を使用し、パラメータを 1 つだけ受け入れます
。
if (have_posts()) : while (have_posts()) : the_post(); var_dump(get_post_custom()); endwhile; endif;
array(4) { [“_edit_last”]=> array(1) { [0]=> string(1) “1” } [“_edit_lock”]=> array(1) { [0]=> string(12) “1342451729:1” } [“_thumbnail_id”]=> array(1) { [0]=> string(3) “228” } [“xzmeta”]=> array(2) { [0]=> string(3) “xz1” [1]=> string(3) “xz2” } }
get_post_custom_values は、現在の記事の指定されたカスタムフィールドの値を取得し、配列の形式で返すために使用されます。
while (have_posts()) : the_post(); var_dump(get_post_custom_values(‘xzmeta')); endwhile; endif;
array(2) { [0]=> string(3) “xz1” [1]=> string(3) “xz2” }
if (have_posts()) : while (have_posts()) : the_post(); var_dump(get_post_custom_keys()); endwhile; endif;
(カスタムフィールドが設定されている場合)
array(4) { [0]=> string(10) “_edit_last” [1]=> string(10) “_edit_lock” [2]=> string(13) “_thumbnail_id” [3]=> string(6) “xzmeta” }
上記では、WordPress 開発における get_post_custom 関数の使用方法の分析を、関連する側面も含めて紹介しました。PHP チュートリアルに興味のある友人に役立つことを願っています。