• 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)
  • 1.
    When you use echo, you must pay attention to the quotes and escape them if necessary.

    E.g. echo "Nubloo said "try this""; won’t work. This will echo everything from the opening ” to the next “, because it will assume you close the echo and wait for a PHP call next, and try this is obviously not a PHP call.

    You can escape characters in PHP by adding \ right before them. This way, they will be treated as characters to be echoed rather than code.

    The above example, correctly:
    echo "Nubloo said \"try this\"";

    You can also use the single quotes with echo, which will escape all content automatically until the next single quote:

    echo 'Nubloo said "try this"';

    2.
    Your line,

    echo "<img src='<?php bloginfo('stylesheet_directory'); ?>/images/top-bnr/<?php echo get_post_meta($post->ID, "header_image", true); ?>' alt='123' />";

    won’t work because you have a PHP call (actually two) within a PHP call. I marked them bold.

    3.

    My Question is, this opens and closes 3 php call’s

    <?php CODE ?> is not a call by itself, but tells the server that the following code is in PHP language. Here, CODE could be a call. It is not wrong to close and open PHP code like in the 1st example you posted. It’s in fact easier to do so, because you don’t have to escape all the characters in an echo.

    Thread Starter Karim Marucchi

    (@pirazo)

    nublooo, thank you for the explanation, I am new and learning.

    Last part to this question, (just to know)

    My reason for asking how to get the code to work without opening and closing 3 instances, is to try and make it “neat” in Dreamweaver. Leaving it 3 instances creates havoc in Dreamweaver for “Wysiwyg”

    Is there a correct way to make this a 1 instance call? I have been looking at the codex, and php.net, but am slowly learning.

    Thanks for the info and time.

    Hi pirazo,

    OK in that case, you have to separate the PHP calls within the echo from the markup or text, following this structure:

    echo ‘HTML Markup’, PHP call, ‘Next HTML Markup’, next PHP call, ‘…’;

    and you have to strip the PHP calls from the <?php ; ?>.

    Try this code:

    <?php
    if (get_post_meta($post->ID, 'header_image', true)) {
    	// if the have custom feild for header image, then use it
    	echo '<img src="', bloginfo('stylesheet_directory'), '/images/top-bnr/', get_post_meta($post->ID, "header_image", true), '" alt="123" />';
    } else {
    	// otherwise, use this default image
    	echo '<img src="', bloginfo('stylesheet_directory'), '/images/top-bnr/top-bnr-001.jpg" width="900" height="260" border="0" /></a>';
    }
    ?>

    I didn’t test it, so let me know if it works 🙂

    Cheers

    EDIT (there was a missing ‘ . Maybe 30 seconds after posting, so try to copy it again now if you tried it already!)

    Oh and by the way, Dreamweaver and WYSIWYG in general – noooooooo! 😉

    Thread Starter Karim Marucchi

    (@pirazo)

    Hello Again Nublooo,

    That worked perfectly, THANK YOU! I will as you suggested, keep the first instance, i am just trying to learn this.

    and yes, in general I agree, just trying to drive the designers crazy too…

    Glad to see it worked Pirazo!

    Good luck with learning PHP, you’ll find it is actually not that hard once you’ve got the hang of it. You gotta start somewhere 🙂

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Correct php syntax for custom field “if – else”’ is closed to new replies.