2016.11.19
【WordPressプラグイン】「Smart Custom Fields」で繰り返し設定した項目や画像を出力する方法
カスタムフィールドで繰り返し登録したい項目があるとき等に便利なプラグイン「Smart Custom Fields」
繰り返し設定したテキストフィールドや画像を出力する方法をメモメモ。
▼カスタム投稿(custom_post)で出力する場合
<?php
// カスタムループを生成し、記事を取得
$args = array(
'post_type' => 'custom_post',//カスタム投稿名を記載
'posts_per_page' => 5,//取得する記事数を記載
'post_status' => 'publish',
'paged' => $paged
);
?>
<?php $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) :?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
// 特定のグループのデータを取得(例:グループ名:repeat_group の場合)
$get_group = SCF::get( 'repeat_group' );
foreach ( $get_group as $fields ) {
?>
<?php echo esc_html( $fields['cf_text'] ); ?>
<?php echo wp_get_attachment_image($fields['cf_img'], 'medium');?>
<?php } ?>
<?php endwhile;?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
【参考】WordPress のカスタムフィールドを簡単・便利に使えるようになるプラグイン「Smart Custom Fields」作った。