Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am displaying related posts with the below code:

<div class="my-related-posts"><h4>You may also be interested in...</h4><ul    
class="relatedposts">
<?php
$posts = get_posts('numberposts=4&orderby=rand&category='. $category->term_id 
. '&exclude=' . $current_post);
foreach($posts as $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; ?><?php endforeach; ?>
</ul>
</div> 

...but I want to make sure it displays posts even if no others in the same catagory...so something like

if (less than 4 related posts) {
     display any other posts from other catagories
}

Thanks for your help.

share|improve this question
    
just a remark: don't forget to use setup_postdata($post) and wp_reset_postdata() with your 'foreach' loop - codex.wordpress.org/Template_Tags/get_posts#Examples –  Michael Mar 4 '13 at 12:27
    
Thanks but I cannot get it to work yet - I have put: if(count($posts)<4){ // your alternative: $posts = get_posts('numberposts=4&orderby=rand&exclude=' . $current_post); } else { $posts = get_posts('numberposts=4&orderby=rand&category='. $category->term_id . '&exclude=' . $current_post); } ....is this the correct way to put as if statement? Thanks. –  user1684099 Mar 9 '13 at 9:55
add comment

2 Answers 2

up vote 1 down vote accepted

if there are no 4 related posts, the code below will fill the number with random posts of random categories, and avoid duplicates:

        <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>
share|improve this answer
    
Wow thanks for this but it's giving me a syntax error - on a curly bracket after all this code. I cannot spot it - can you help? Thanks again! –  user1684099 Mar 11 '13 at 9:26
    
in my test, the suggested code does not generate a syntax error; this might be coming from some other code within your template. please use the pastebin to show your full code. –  Michael Mar 11 '13 at 9:55
    
Hi Michael, sorry new here...how to I get to the pastebin! –  user1684099 Mar 11 '13 at 10:37
    
pastebin.com - paste your code, click submit, copy the new url from the browser and post this here. –  Michael Mar 11 '13 at 12:29
    
Thanks Michael, link is pastebin.com/WGA2aVbF –  user1684099 Mar 11 '13 at 13:29
show 2 more comments

It sounds like you are talking about this kind of setup:

if(count($posts)<4){
    // your alternative:
    $posts = get_posts('numberposts=4&orderby=rand&exclude=' . $current_post);
}

for this part:

if (less than 4 related posts) {
     display any other posts from other catagories
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.