如果没有4个相关帖子,下面的代码将用随机类别的随机帖子填充数字,并避免重复:
<div class="my-related-posts"><h4>You may also be interested in...</h4><ul class="relatedposts">
<?php
$duplicates = array( $current_post );
$posts = get_posts( array( \'posts_per_page\' => 4, \'orderby\' => \'rand\', \'category\' => $category->term_id, \'post__not_in\' => array( $current_post )) );
$second_numbers = (!$posts) ? 4 : (4 - count( $posts ));
if( $posts ) :
foreach($posts as $post) :
setup_postdata( $post );
$duplicates[] = $post->ID;
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ));
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?><br /><img src="<?php echo esc_url( $image[0] ); ?>" alt="View more info" /></a></li>
<?php endforeach; wp_reset_postdata();
endif;
if( $second_numbers >=1 ) :
$posts = get_posts( array(\'posts_per_page\' => $second_numbers, \'orderby\' => \'rand\', \'post__not_in\' => $duplicates ) );
if( $posts ) foreach($posts as $post) :
setup_postdata( $post );
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ));
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?><br /><img src="<?php echo esc_url( $image[0] ); ?>" alt="View more info" /></a></li>
<?php endforeach; wp_reset_postdata();
endif; ?>
</ul>
</div>