0

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.

2
  • 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, 2013 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. Mar 9, 2013 at 9:55

1 Answer 1

1

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>
5
  • 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! Mar 11, 2013 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, 2013 at 9:55
  • Hi Michael, sorry new here...how to I get to the pastebin! Mar 11, 2013 at 10:37
  • pastebin.com - paste your code, click submit, copy the new url from the browser and post this here.
    – Michael
    Mar 11, 2013 at 12:29
  • the endforeach of the 'categories' loop was missing: pastebin.com/Cw8bZced
    – Michael
    Mar 11, 2013 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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