###前提・実現したいこと
カスタム投稿タイプ(media_info)のタクソノミー(media_info)のターム別の記事一覧ページを作っており、各タームごとに2件ずつyoutubeのiframeが表示されるようになっています。このyoutubeのiframeの動画IDがカスタム投稿タイプのmedia_youtubeというスラッグに格納されています。
そこで、2件を超える投稿数を持つタームについてはmoreボタンを設置して更に表示させようと企てています。
下記のようなソースコードを書いたのですが、2件を超えないタームにもmoreボタンが表示されてしまいます。
どうコードを修正すればよいでしょうか?
また、moreのリンク先についても知見を頂きたいです。そのリンク先を出力するにはどうすればよいでしょうか。
taxonomy-{投稿タイプ名}.phpをつくってその中に標準のループがあればよいのでしょうか?
超初心者なので部分部分でしか理解しておりません。
よろしくお願いします。
###ソースコード
<?php $args = array( 'orderby' => 'term_order', 'order' => 'ASC', 'parent' => 0, 'hierarhical' => 0 ); $my_taxonomy = 'media_info'; $terms = get_terms($my_taxonomy,$args); $count = count( $terms ); if ( ! empty( $terms ) && ! is_wp_error( $terms )): foreach ( $terms as $term ): ?> <h3><?php echo esc_html($term->name); ?></h3> <?php $tax_posts = new WP_Query(array( 'post_type' => 'media_info', 'posts_per_page' => 2, 'tax_query' => array( array( 'taxonomy' => 'media_info', 'terms' => array($term->slug), 'field' =>'slug', ) ), )); if ($tax_posts->have_posts()) : ?> <div class="media-cat"> <?php while ($tax_posts-> have_posts()) : $tax_posts->the_post(); ?> <div class="media_info"> <iframe src="https://www.youtube.com/embed/<?php the_field('media_youtube'); ?>" frameborder="0" allowfullscreen></iframe> </div> <?php endwhile; ?> <div class="clrfx"></div> <?php wp_reset_postdata(); ?> <?php if($count >= 3): ?> //この部分についての質問 <a href="#"><div class="media-more">More</div></a> <?php endif; ?> </div> <?php endif; endforeach; endif; ?>
###補足情報(言語/FW/ツール等のバージョンなど)
wordpress 4.6.1を使っています。
###追記
<?php $args = array( 'orderby' => 'term_order', 'order' => 'ASC', 'parent' => 0, 'hierarhical' => 0 ); $my_taxonomy = 'media_info';//カスタム分類(カスタムタクソノミー) $terms = get_terms($my_taxonomy,$args); if ( ! empty( $terms ) && ! is_wp_error( $terms )): foreach ( $terms as $term ): ?> <h3><?php echo esc_html($term->name); ?></h3> <?php $count = $term->count; //タームの記事数取得 $link = get_term_link($term->slug, $term->taxonomy); //タームのリンクを取得 $tax_posts = new WP_Query(array( 'post_type' => 'media_info', 'posts_per_page' => 2, 'tax_query' => array( array( 'taxonomy' => 'media_info',//カスタム分類(カスタムタクソノミー) 'terms' => array($term->slug), 'field' =>'slug', ) ), )); if ($tax_posts->have_posts()) : ?> <div class="media-cat"> <?php while ($tax_posts-> have_posts()) : $tax_posts->the_post(); ?> <div class="media_info"> <iframe src="https://www.youtube.com/embed/<?php the_field('media_youtube'); ?>" frameborder="0" allowfullscreen></iframe> </div> <?php endwhile; ?> <div class="clrfx"></div> <p><?php echo $term->description; ?></p> <?php wp_reset_postdata(); ?> <?php if($count >= 3): ?> //タームの記事数が3以上ならmoreボタンを表示 <a href="<?php echo $link; ?>"><div class="media-more">More</div></a> <?php endif; ?> </div> <?php endif; endforeach; endif; ?>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/09 13:51
2017/04/10 01:37
退会済みユーザー
2017/04/10 07:50
2017/04/11 09:13 編集
退会済みユーザー
2017/04/12 07:47
2017/04/12 11:30
退会済みユーザー
2017/04/12 11:47
2017/04/13 00:18
退会済みユーザー
2017/04/13 03:14
2017/04/13 03:35