I'm trying to figure out how to do a if else statement in Wordpress. I want it to state if $data[0]['media_upload'] is empty then show the blog title. Else (if there is something there) show the logo in Wordpress. I'm wondering if I have the wrong syntax.

Right now I have the following which is showing the blog title and no errors but I do have a image uploaded and its not showing for some reason. Any ideas would be great!

<?php
if (empty($data[0]['media_upload'])) {
echo'<h1 class="site-title"><a href="';
  esc_url( home_url( '/' ) );
echo'" title="';
  esc_attr( get_bloginfo( 'name', 'display' ) );
echo'" rel="home">';
  bloginfo( 'name' );
echo'</a></h1>';
}

else{
echo '<a href="';
  bloginfo('siteurl');
    echo '">';
echo'<img src="';
  global $data; 
  $data['media_upload']; 
echo'" /></a>';
}

?>
有帮助吗?

解决方案 2

it should be

echo'<img src="';
global $data; 
$data[0]['media_upload']; 
      ^ // you forgot 0th index
echo'" /></a>';

其他提示

Try changing: $data['media_upload']; in your else to: $data[0]['media_upload']; like you have in your if statement (or the other way around). We don't really know what $data is so it's hard to help you.

Edit: Since I can't comment yet, I'll comment here. Try using the rubber duck technique; explain to us what is happening with the $data object in your code. Maybe you'll figure it out yourself.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top