wordpress - Any Ideas Why this PHP If / Else Function Doesn't Work? -
i've written 2 functions work on wordpress site , having trouble second one. first function scans post image. if finds one, selects main image of site. if there's no image, defers default image. here's 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 default image $first_img = "http://www.example.com/image.jpg"; } return $first_img; } the code above works fine. second part, i'm trying specify if default image used, twitter card should set "summary." if image found, twitter card should set "summary_large_image."
here's 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, returns "summary," whether listed image used in post or not.
if know why second function doesn't work, i'd love insight.
thanks!
you need == not =.
== comparison
= assignment
as $first_image, make variable outside function , in function write:
try this:
function twitter_card() { global $post, $posts; if (main_image() == "http://www.example.com/image.jpg") { echo "summary"; } else { echo "summary_large_image"; } }
Comments
Post a Comment