各タームの特定の記事を1件ずつ表示
-
【現状】
ポストタイプ名がmenu、タクソノミー名がmenucategory、termがそれぞれ10個ほどあります。Advance Custom Fieldsを使用しています。【実現したい事】
アーカイブページに各タームから1つずつ記事をピックアップし、合計10の記事を表示させたい。functions.phpの記述
function customize_menu_query($query) { if ( is_admin() || ! $query->is_main_query() ) return; if ( $query->is_post_type_archive('menu') && $query->is_main_query() ) { $query->set( 'post_type', 'menu' ); $query->set( 'posts_per_page', '1' ); $tax_query[] = array( 'taxonomy' => 'menucategory', 'terms' => array('kani','obentou','drink','course','fugu','lunch','tanpin','kisetsu','niku','nabe'), 'field' => 'slug', 'operator' => 'IN'); $query->set( 'tax_query', array('relation' => 'AND' , $tax_query) ); } } add_action( 'pre_get_posts', 'customize_menu_query' );
archive-menu.phpの記述
<?php if(have_posts()): ?> <div class="foodbox"> <?php while(have_posts()): the_post(); ?> <div class="fee"> <h4><?php the_title(); ?></h4> <p><span>料金</span><?php the_field('price'); ?></p> <p><?php the_field('txt'); ?></p> </div> <?php $attachment_id = get_field('photo'); $size = "custom_size"; // $image = wp_get_attachment_image_src( $attachment_id, $size ); $attachment = get_post( get_field('photo') ); $alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true); $image_title = $attachment->post_title; ?> <img src="<?php echo $image[0]; ?>" width="<?php echo $image[1]; ?>" height="<?php echo $image[2]; ?>" alt="<?php the_title(); ?>" /> <?php endwhile; ?> </div> <?php else: endif; ?>
上記の方法だとポストタイプmenuの最新投稿が1件表示されるだけになってしまいます。
各タームの特定の記事を表示したい場合、何処かにカスタムフィールドを追加し紐づけてピックアップすればいいのでしょうか?
また、その際の記述はどのようにすればよろしいのでしょうか?
アドバイスよろしくお願いします。
3件の返信を表示中 - 1 - 3件目 (全3件中)
3件の返信を表示中 - 1 - 3件目 (全3件中)
- トピック「各タームの特定の記事を1件ずつ表示」には新たに返信することはできません。