DEV Community

kumamon
kumamon

Posted on

1

【WordPress】同じカテゴリ内でランダムに関連記事生成

<div class="newcontentarea">
<?php
$categories = get_the_category($post->ID);
$category_ID = array();
foreach($categories as $category):
  array_push( $category_ID, $category -> cat_ID);
endforeach ;
$args = array(
  'post__not_in' => array($post -> ID), // 今読んでいる記事を除く
  'posts_per_page'=> 6,
  'category__in' => $category_ID,
  'orderby' => 'rand', // ランダムに記事を選ぶ
);
$query = new WP_Query($args);
if( $query -> have_posts() ): while ($query -> have_posts()) : $query -> the_post();
?>
<div class="newcontentarea2">
<a href="<?php the_permalink();?>">
    <div class="newcontentarea3">
        <div class="innerarea">

    <?php if(has_post_thumbnail()): ?> 
    <?php the_post_thumbnail(); ?>
<?php else: ?>
    <img class="lazyload" data-src="★★NOIMAGEパス★★" alt="no image">
<?php endif; ?>
    <h3><?php the_title();?></h3>
        </div>
</div>
    </a>
 </div>       
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>

</div>

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay