1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| <?php
$current_post_id = get_the_ID();
$categories = get_the_category($current_post_id); if ($categories) { $category_ids = array(); foreach ($categories as $category) { $category_ids[] = $category->term_id; }
$args = array( 'category__in' => $category_ids, 'post__not_in' => array($current_post_id), 'posts_per_page' => 6, 'ignore_sticky_posts' => 1, 'orderby' => 'rand', );
$related_query = new WP_Query($args);
if ($related_query->have_posts()) : echo '<h3>相关文章</h3>'; echo '<ul class="related-posts">'; while ($related_query->have_posts()) : $related_query->the_post(); ?> <li> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php if (has_post_thumbnail()) { the_post_thumbnail('thumbnail'); } the_title(); ?> </a> </li> <?php endwhile; echo '</ul>'; wp_reset_postdata(); else : echo '<p>暂无相关文章</p>'; endif; } ?>
|