カスタムフィールドの値が特定の記事を出力する
Advanced Custom Fieldsで作ったカスタムフィールドを使っています。
カスタム投稿sample_01とsample_02で、真/偽の値が真(true)だった場合の記事を4件出力する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <div class="section_inner"> <h2>カスタム投稿 真/偽での振り分け</h2> <div class="contents"> <?php $args = array( 'post_type' => array('sample_01','sample_02'),/*カスタム投稿タイプの名前*/ 'showposts' => 4, 'meta_key'=>'new',/*カスタムフィールドのフィールド名*/ 'meta_value'=>true,/*カスタムフィールドの値*/ ); $customPosts = get_posts($args); if($customPosts) : foreach($customPosts as $post) : setup_postdata( $post ); ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?> <div class="title"><?php the_field('title'); ?></div> <div class="thumbnail"><?php echo wp_get_attachment_image(get_post_meta($post->ID, 'thumbnail', true),'thumbnailSmall'); ?></div> </a> <?php endforeach; ?> <?php else : ?> <p>記事はありません。</p> <?php endif; wp_reset_postdata(); ?> </div> </div> |