• I have a div box in my sidebar that links to a random post and also pulls a picture from that same post. However, sometimes that link is the same as the post you are currently on. I want to avoid that.

    I started off with this code:

    <?php
        	$my_posts = get_posts('category=3&numberposts=1&orderby=rand');
    	foreach($my_posts as $post) :
    		setup_postdata($post);
    ?>
    <img src="/images/<?php the_title(); ?>.jpg" title="Rate Me" height=100 width=100 border=0 alt="">
    <br>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>

    Then when I started tweaking the code:

    <?php
        	$my_posts = get_posts('category=3&numberposts=1&orderby=rand');
    	$thisone = the_title();
    	echo $thisone;
    
    	foreach($my_posts as $post) :
    		setup_postdata($post);
    		$thatone = the_title();
    
    		echo $thatone;
    		echo $thisone;
    ?>
    <img src="/images/<?php the_title(); ?>.jpg" title="Rate Me" height=100 width=100 border=0 alt="">
    <br>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>

    The variable $thisone returns the title of the current post’s title. And it does successfully. The variable $thatone returns the random link’s title successfully. But the second time I echo “$thisone” it does not show up.

    Why this is important is because I want to test $thisone vs $thatone to make sure they are not equal. How can I do this?

    Your help is appreciated.

  • The topic ‘Test variables of current post against ones from other posts’ is closed to new replies.