Correct php syntax for custom field “if – else”
-
Hello,
Please excuse my newbieness to PHP, i am really tying hee, please be kind.
After reading this post on Creating an “if exists” for custom field keys I created the following code that seems to work for having a default header image if one is not defined in the custom fields.
<?php if (get_post_meta($post->ID, 'header_image', true)) { // if the have custom feild for header image, then use it ?> <a href="<?php echo get_settings('home'); ?>/"> <img src="<?php bloginfo('stylesheet_directory'); ?>/images/top-bnr/<?php echo get_post_meta($post->ID, "header_image", true); ?>" width="900" height="260" border="0" /> </a> <?php } else { // otherwise, use this default image ?> <img src="<?php bloginfo('stylesheet_directory'); ?>/images/top-bnr/top-bnr-001.jpg" width="900" height="260" border="0" /> <?php } ?>My Question is, this opens and closes 3 php call’s, I believe(and I might be wrong) that it is correct to try to make this all one call, (using echo’s?). I do not know enough php to fix the code below to do the same as the code above:
<?php if (get_post_meta($post->ID, 'header_image', true)) { // if the have custom feild for header image, then use it echo "<img src='<?php bloginfo('stylesheet_directory'); ?>/images/top-bnr/<?php echo get_post_meta($post->ID, "header_image", true); ?>' alt='123' />"; } else { // otherwise, use this default image echo "<img src='<?php bloginfo('stylesheet_directory'); ?>/images/top-bnr/top-bnr-001.jpg' width='900' height='260' border='0' /></a>"; } ?>Please help point me in the right direction.
Thank you.
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
The topic ‘Correct php syntax for custom field “if – else”’ is closed to new replies.