Support » Fixing WordPress » php if, else & custom fields – syntax error

  • Resolved abcdefghijklmnop

    (@abcdefghijklmnop)


    Hi all,

    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!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Can you try the following:

    <?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 elseif ( 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; ?>

    docs: http://php.net/manual/en/control-structures.alternative-syntax.php

    Thread Starter abcdefghijklmnop

    (@abcdefghijklmnop)

    Hi @jitdendrajoshi .. thanks for taking the time to reply. In the end, i found the answer, the below code worked for me…

    <?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 } 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 endif; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘php if, else & custom fields – syntax error’ is closed to new replies.