Forums

Difference in two working PHP solutions get_post_meta (2 posts)

  1. bjorneric
    Member
    Posted 10 months ago #

    Hi,

    Iam setting up a new site using 3-5 custom fields.

    Basic function is that if the custom field is filled in it will show for example the image linked - if its not filled in it will output nothing.

    Being fairly new to PHP i have made two different solutions - both working and would like your input och what the differences are?

    Solution 1 (inside the loop)

    <?php if ( $image=get_post_meta($post->ID, 'image', true) ) : ?>
    <div class="image-container">
    <img src="<?php echo $image; ?>" /></div>
    <?php endif; ?>

    Solution 2 (inside the loop)

    <?php $image=get_post_meta($post->ID, 'image2', true); ?>
    <?php if ( $image2 ) : ?>
    <div><img src="<?php echo $image2; ?>" /></div>
    <?php endif; ?>

    The most obvious being that the second one could be added with else statements - any other thoughts? The first one being much shorter and therefore prefered?

  2. bjorneric
    Member
    Posted 10 months ago #

    *edited*

Reply

You must log in to post.

About this Topic