Define variables inside while loop?

I am working with the code below. I need to make a “related posts” section. I can’t figure out how to declare a variable inside a while loop.

Basically, I need to check if the post has a thumbnail, if not I need to set it to a default thumbnail instead (I made a function for this of post_default_thumb(); )

I tried setting the variable like I would in a foreach loop, but it just dumps the variable no matter what.

I tried:

        <?php $post_thumbnail_img = the_post_thumbnail();
			if ($post_thumbnail_img){
			$related_thumb = $post_thumbnail_img; }
			else {
            $related_thumb = post_default_img();
            }
        	var_dump ();
        	?>
        
<?php
//for use in the loop, list 3 post titles related to first tag on current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo '<h5> Related Posts </h5>';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'posts_per_page'=>3,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>

        
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> 
<?php the_post_thumbnail(); ?>
        </a>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> 
<?php the_title(); ?></a>
 
<?php
endwhile;
}
wp_reset_query();
}
?>

Same way you declare any other variable. A while loop is not a new scope of reference in PHP. Variables declared inside the loop are part of the scope that contains the loop.

var_dump needs a context for it to dump. You would var_dump($related_thumb);.

I have something screwed up, because the code below dumps out a single iteration like I would expect ( a related post), but before that, it’s dumping out the $post_thumbnail_img AND post_default_img() (with no other post data such as the title.

<?php
//for use in the loop, list 3 post titles related to first tag on current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo '<h5> Related Posts </h5>';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'posts_per_page'=>3,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
        
              <?php $post_thumbnail_img = the_post_thumbnail();
			if ($post_thumbnail_img){
			$related_thumb = $post_thumbnail_img; }
			else {
            $related_thumb = post_default_img();
            }
        	var_dump ($related_thumb);
        	?>

        
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> 
<?php the_post_thumbnail(); ?>
        </a>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> 
<?php the_title(); ?></a>
 
<?php
endwhile;
}
wp_reset_query();
}
?>

If I’m following this right, you first dump the $related_thumb, which will either be the thumb or a default image, then here:-

…You call the the_post_thumbnail() function regardless of its outcome.

I don’t know the content of this function, but could not the logic which determines whether to use the default image or not be built into it?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.