PHP: Variables based on if statements?

I’m completely new to PHP, so bear with me.

I am trying to set a variable based on an if else statement. It’s breaking my site, when I try it, so I am assuming it’s not possible.

Something like…

  <?php
			
 
			if ( has_post_thumbnail($some_post['ID']) ) {
    			$featured_img_url = get_the_post_thumbnail_url($some_post->ID, 'full'); 
			}
			else {
    		echo '<img src="' . get_stylesheet_directory_uri() . '/assets/img/hero-thumbnail-default.jpg" />';
        	
            }?>

Essentially, I am using get_recent_posts to pull in some posts as $some_post and I need to check if $some_post has a featured image, if not, I need to echo the fallback image.

How? What happens if you execute the if statement?

Just in general:
This if statement is a story of used car salesmen.:

“If true, put a key in the box for the next test drive.
Else, talk about the price of tea in china.”

Why is your false condition echoing, when the true is setting a variable? Either both should be setting the variable, or both should be echoing.

1 Like

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