Any Ideas Why this PHP If / Else Function Doesn’t Work?

I’ve written two functions to work together on a WordPress site and am having trouble with the second one.
This is the first function scans the post for an image. If it finds one, it selects as the main image of the site. If there’s no image, it defers to a default image.
Here’s the function:

    function main_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src=['"]([^'"]+)['"].*>/i', $post->post_content, $matches); $first_img = $matches [1] [0];

if(empty($first_img)){ //Defines a default image $first_img = “http://www.example.com/image.jpg“; } return $first_img; }

The code above works fine. For the second part, I’m trying to specify that if the default image was used, the Twitter card should be set to “summary.” If another image was found, the Twitter card should be set to “summary_large_image.”

Here’s the function:

    function twitter_card() { global $post, $posts; if ($first_img = "http://www.example.com/image.jpg") { echo "summary"; } else { echo "summary_large_image"; } }

The second function isn’t working correctly, as it always returns “summary,” whether the listed image is used in the post or not.

If you know why the second function doesn’t work, I’d love some insight.

Thanks!

Read more here: Any Ideas Why this PHP If / Else Function Doesn’t Work?

Leave a Reply

Your email address will not be published. Required fields are marked *