treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Default image in custom field

  • Hello guys,

    I´m looking for a solution to have a default image when this custom field (wordpress) it is not in use.

    	<img src='<?php echo get_post_meta($post->ID, 'imagen', true); ?> '/>


    Any idea?
    Many thanks.
    Rai.
  • <?php
    $img = get_post_meta($post->ID, 'imagen', true);

    if ($img) {
    echo '<img src="<?php echo get_post_meta($post->ID, 'imagen', true); ?>" />';
    } else {
    echo '<img src="path/to/default/image.jpg" />';
    }
    ?>
  • Thanks Doc,

    But it show me this error:

    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/content/57/6689957/html/azahararomaterapia/wp-content/themes/azahar/pagina-producto.php on line 19


  • Oops! Terrible copy/pasting!
    <?php
    $img = get_post_meta($post->ID, 'imagen', true);

    if ($img) {
    echo '<img src="'. $img . '" />';
    } else {
    echo '<img src="path/to/default/image.jpg" />';
    }
    ?>

    Give that a go!
  • Excellent Doc!,

    Can you recommend something to read to learn the code at that level?.

    Thanks.
  • That was actually just a modified version of a snippet from this very site! http://css-tricks.com/snippets/wordpress/using-custom-fields/

    As far as suggestions on where to learn, I don't really have any. It's just repetition, practice practice practice.

    Try to not take on too much at once or else none of it will stick. Focus on a few features at a time and really try to master them before you move on to something else.
  • Thanks Doc!.