0

I'm just starting to use custom fields and would like to use it to display an author box with (1) author photo (2) author name (3) author bio.

I use this code in loop-single.php and it works fine:

<?php if ( get_post_meta($post->ID, 'author-photo', 'author-name', 'author-bio', true) ) : ?>
<div class="authorbox">
    <div class="authorphoto">
    <img src="<?php $key="author-photo"; echo get_post_meta($post->ID, $key, true); ?>" />
    </div>
        <div class="authorbox-content">
            <h3 class="author-name"><?php $key="author-name"; echo get_post_meta($post->ID, $key, true); ?></h3>
            <p><?php $key="author-bio"; echo get_post_meta($post->ID, $key, true); ?></p>
        </div>
</div>
<?php endif; ?>

Basically, the above displays author photo, name and bio if all 3 custom field values exist.

Next, I tried to make it display only the author name and bio if the photo value does not exist. So, I add some code right below the above code and the resulting entire code looks like this:

<?php if ( get_post_meta($post->ID, 'author-photo', 'author-name', 'author-bio', true) ) : ?>
<div class="authorbox">
    <div class="authorphoto">
    <img src="<?php $key="author-photo"; echo get_post_meta($post->ID, $key, true); ?>" />
    </div>
        <div class="authorbox-content">
            <h3 class="author-name"><?php $key="author-name"; echo get_post_meta($post->ID, $key, true); ?></h3>
            <p><?php $key="author-bio"; echo get_post_meta($post->ID, $key, true); ?></p>
        </div>
</div>
<?php endif; ?>


<?php if ( get_post_meta($post->ID, 'author-name', 'author-bio', true) ) : ?>
<div class="authorbox">
        <div class="authorbox-content-nophoto">
            <h3 class="author-name"><?php $key="author-name"; echo get_post_meta($post->ID, $key, true); ?></h3>
            <p><?php $key="author-bio"; echo get_post_meta($post->ID, $key, true); ?></p>
        </div>
</div>
<?php endif; ?>

Now, this works fine if only the author name and bio values exist. However, if all 3 values exist i.e. photo, name and bio, then a problem arises... there will be 2 author boxes displayed (one with photo and one without photo).

How may I solve this problem? (I only need one author box displayed at any one time, either with photo or without photo).

Thanks in advance!

PS: I have tried adding 'else' but am not sure of the exact way to code it, I've received syntax errors when trying to add 'else' in between the code. Any pointers would be greatly appreciated!

0

There are a couple of ways to do it. Try:

<?php if ( get_post_meta($post->ID, 'author-photo', 'author-name', 'author-bio', true) ) { ?>

    <div class="authorbox">
        <div class="authorphoto">
            <img src="<?php $key="author-photo"; echo get_post_meta($post->ID, $key, true); ?>"     />
        </div>
        <div class="authorbox-content">
            <h3 class="author-name"><?php $key="author-name"; echo get_post_meta($post->ID, $key, true); ?></h3>
            <p><?php $key="author-bio"; echo get_post_meta($post->ID, $key, true); ?></p>
        </div>
    </div>

<?php } ?>

<?php else { ?>

    <?php if ( get_post_meta($post->ID, 'author-name', 'author-bio', true) ) { ?>
        <div class="authorbox">
            <div class="authorbox-content-nophoto">
                <h3 class="author-name"><?php $key="author-name"; echo get_post_meta($post->ID, $key, true); ?></h3>
                <p><?php $key="author-bio"; echo get_post_meta($post->ID, $key, true); ?></p>
            </div>
        </div>
    <?php } ?>

<?php } ?>
  • thanks for taking the time to answer, this works! except that I had to change these 2 lines: <?php } ?> <?php else { ?> to this one line: <?php } else { ?> ...thanks once again! saved me my whole sunday :) – publicinformation Aug 30 '15 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.