プラグインなしで関連記事を表示する方法について
-
<div class="other-box"> <h4>OTHER ARTICLE</h4> <div class="general-blog"> <?php //表示する関連記事の件数を指定 $postnum = 2; //現在のページのカテゴリを取得 $categories = get_the_category($post->ID); $category_ids = array(); foreach($categories as $category) : array_push($category_ids, $category->cat_ID); endforeach; $args = array( 'post_type' => 'post', //投稿タイプを指定 'post_per_page' => $postnum, //取得する記事数を指定 'category__in' => $category_ids, //現在のカテゴリの投稿にフィルタ 'post__not_in' => array($post->ID), //現在のページを除外 'orderby' => 'rand' //ランダムに投稿を取得 ); $the_query = new WP_Query($args); if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?> <div class="blk"> <a href="<?php the_permalink(); ?>"> <div class="photo"><?php the_post_thumbnail('full'); ?><span><?php the_time('Y.m.d'); ?></span></div> <dl> <dt><?php $cat = get_the_category(); $cat = $cat[0]; { echo $cat->cat_name; } ?></dt> <dd><h3><?php echo get_the_title(); ?></h3></dd> </dl> </a> </div> <?php endwhile; endif; wp_reset_postdata(); ?> </div> </div>
現在、上記のコードを使い関連記事を表示させています。
しかし、関連するカテゴリーがほかにない場合「OTHER ARTICLE」という部分だけ表示されます。
関連記事がない場合は、<div class=”other-box”>~</div>を丸ごと非表示にしたいのですが、
どのように記述すれば、よいでしょうか。
1件の返信を表示中 - 1 - 1件目 (全1件中)
1件の返信を表示中 - 1 - 1件目 (全1件中)
- トピック「プラグインなしで関連記事を表示する方法について」には新たに返信することはできません。