LoginSignup
0
0

More than 3 years have passed since last update.

Wordpress 同じカテゴリの記事を関連記事として出すタグのテンプレ

Last updated at Posted at 2021-05-18

コピペでどうぞ

<?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'=> 10,//件数指定
    'category__in' => $category_ID,//現在の記事と同じカテゴリ
    'orderby' => 'rand',//ランダムに記事を選ぶ
);
$query = new WP_Query($args);
if( $query -> have_posts() ):
    while ($query -> have_posts()) :
    $query -> the_post();
?>
<!--ループ-->
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?><?php the_title(); ?></a>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?> 
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0