Forum Moderators: coopster

Message Too Old, No Replies

Wordpress Related Posts if else statement

Wordpress Related Posts if else statement

         

cyberpunkstudio

12:03 pm on Jul 10, 2009 (gmt 0)

10+ Year Member



Hi I am using Wordpress I found this script that will display 5 related posts to the particular blog post based on tags:


<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo '';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>5,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" title="Read more on <?php the_title_attribute(); ?>"><img src="<?php echo get_post_meta($post->ID, "Thumbnail", true);?>" alt="<?php the_title(); ?>" width="36" height="36" class="thumbnail" /></a>

<a href="<?php the_permalink() ?>" title="Read more on <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</li>

<?php
endwhile;

}
}


?>

I don't know php myself. However when there are no related posts it doesn't display anything. How do I add a if statement so that when there are no related posts it displays: <p>There are currently no related posts</p>

Hope you can help

:-)

andrewsmd

4:09 pm on Jul 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This should do it
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo '';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>5,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>"
title="Read more on <?php the_title_attribute(); ?>"><img
src="<?php echo get_post_meta($post->ID, "Thumbnail", true);?>"
alt="<?php the_title(); ?>" width="36" height="36" class="thumbnail" /></a>

<a href="<?php the_permalink() ?>"
title="Read more on <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</li>

<?php
endwhile;

}
else{
echo("<p>There are currently no related posts</p>");
}
}

cyberpunkstudio

4:44 pm on Jul 10, 2009 (gmt 0)

10+ Year Member



Hi thanks for the script. I just tried it, it doesnt work nothing appears.

You also missed the final closing php tag. But I dont know why but the <p>There are currently no related posts</p> doesnt appear.

Are you sure this is correctly written did you miss anything by any chance?

cyberpunkstudio

4:51 pm on Jul 10, 2009 (gmt 0)

10+ Year Member



Hi sorry, just discovered the script does work!

:-)

Just that some of my blog post didnt have any tags this prevents the else statement from appearing, I just added a random tag to the post and it then displayed:

<p>There are currently no related posts</p>

Thanks for your help, it was really helpful

andrewsmd

5:03 pm on Jul 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No problem that's what we're here for.