目次
Advanced Custom Fields通常の出力方法と異なる場合
カテゴリーなどでカスタムフィールドを使った場合や、
固定ページのカスタムフィールドの値を表示させたい場合。
カテゴリーに設定した場合
設定方法は、ルールの部分で「タクソノミー」「等しい」「カテゴリー」を選択します。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $categories = get_categories(); //$categories = get_categories('parent=0'); foreach($categories as $category) : $cat_id = $category->cat_ID; $post_id = 'category_'.$cat_id; $catimg = get_field('カスタムフィールド名',$post_id); $img = wp_get_attachment_image_src($catimg, 'full'); ?> <div><img src="<?php echo $img[0]; ?>" alt="<?php echo $category->cat_name; ?>" /></div> <?php endforeach; ?> |
カテゴリーにテキストのカスタムフィールドを作成して表示する場合。
1 2 3 4 5 6 7 8 9 10 |
<?php $queried_object = get_queried_object(); $taxonomy = $queried_object->taxonomy; $term_id = $queried_object->term_id; $cat_desc = get_field('カスタムフィールド名', $taxonomy . '_' . $term_id); if ( $cat_desc ) { echo '' . $cat_desc . ''; } ?> |
別の固定ページからカスタムフィールドの値を表示
特定の固定ページで作成したカスタムフィールド の値を取得し表示する方法
1 2 3 4 5 6 7 8 9 10 11 |
<!--1種類目 --> <?php $fields = get_post_custom(固定ページのID); $val = $fields['フィールド名'][0]; echo $val; ?> <!--2種類目 --> <?php echo get_field('フィールド名',固定ページのID); ?> |
参考サイト