简体   繁体   中英

PHP If/Else for Wordpress Custom Fields

I have a site that is using custom fields, and I want to either show the content for the field if the field is being used, and if the field is not being used per a Post then of course don't show the contents of the fields.

Seems my code below is not correct, as it is showing the content in the else block when the Post is not using any of the custom fields.

Any help would be greatly appreciated!

Here is the Post in question: http://beta.ohsinsider.com/insider-top-stories/workers%E2%80%99-compensation-may-not-shield-you-from-lawsuits-by-injured-workers

Here is the Post Edit showing that the field I am calling in my code are not being used (http://screencast.com/t/aBjt1drIw).

I have confirmed that when I do input the value for the custom field is it being outputted in the Post.

Here is the code I am using:

<?php
    $pdfurl = get_post_meta($post->ID, 'pdf', true);
    $wordurl = get_post_meta($post->ID, 'word', true);
    if( !empty($pdf) || !empty($word) ){
    ?>
    <?php /* show nothing then */ } else { ?>
    <div id="post_downloads_box">
        <h3 class="single_related_footer">Dfownload Now</h3>
        <div id="post_downloads_box_left">
            <a target="_blank" href="<?php echo get_post_meta($post->ID, 'pdf', true); ?>"><img src="<?php bloginfo('template_url'); ?>/images/post_pdf_icon.jpg" /></a>
        </div>
        <div id="post_downloads_box_right">
            <a target="_blank" href="<?php echo get_post_meta($post->ID, 'word', true); ?>"><img src="<?php bloginfo('template_url'); ?>/images/post_word_icon.jpg" /></a>
        </div>
    </div>
    <?php } ?>  

Your code block is a little confusing ... partly because your logic fails to take into account what would happen if the post has a PDF custom field and not a Word custom field ... you'll still be displaying both sets of markup. Instead, I would recommend this:

<?php if( get_post_meta($post->ID, 'pdf', true) && get_post_meta($post->ID, 'word', true) ) : ?>

<div id="post_downloads_box">
    <h3 class="single_related_footer">Download Now</h3>
    <div id="post_downloads_box_left">
        <a target="_blank" href="<?php echo get_post_meta($post->ID, 'pdf', true); ?>"><img src="<?php bloginfo('template_url'); ?>/images/post_pdf_icon.jpg" /></a>
    </div>
    <div id="post_downloads_box_right">
        <a target="_blank" href="<?php echo get_post_meta($post->ID, 'word', true); ?>"><img src="<?php bloginfo('template_url'); ?>/images/post_word_icon.jpg" /></a>
    </div>
</div>

<?php endif; ?>

This checks for both custom fields and, if either one is missing, it skips rendering the markup and "shows nothing." If both are there, it renders your markup.

Inverting the logic of the first statement would be the ideal solution.

<?php
$pdfurl = get_post_meta($post->ID, 'pdf', true);
$wordurl = get_post_meta($post->ID, 'word', true);
if( empty($pdf) && empty($word) ) {
?>
    <div id="post_downloads_box">
        <h3 class="single_related_footer">Dfownload Now</h3>
        <div id="post_downloads_box_left">
            <a target="_blank" href="<?php echo get_post_meta($post->ID, 'pdf', true); ?>"><img src="<?php bloginfo('template_url'); ?>/images/post_pdf_icon.jpg" /></a>
        </div>
        <div id="post_downloads_box_right">
            <a target="_blank" href="<?php echo get_post_meta($post->ID, 'word', true); ?>"><img src="<?php bloginfo('template_url'); ?>/images/post_word_icon.jpg" /></a>
        </div>
    </div>
<?php } ?>

Note although this is inverted logic, it doesn't appear to be correct. As this would display download links if both were empty. The following should be your if statement.

if (!empty($pdf) || !empty($word))

Seems this is better:

<?php $values = get_post_custom_values("pdf"); if (isset($values[0])) {?>
    <div id="post_downloads_box">
        <h3 class="single_related_footer">Dfownload Now</h3>
        <div id="post_downloads_box_left">
            <a target="_blank" href="<?php echo get_post_meta($post->ID, 'pdf', true); ?>"><img src="<?php bloginfo('template_url'); ?>/images/post_pdf_icon.jpg" /></a>
        </div>
        <div id="post_downloads_box_right">
            <a target="_blank" href="<?php echo get_post_meta($post->ID, 'word', true); ?>"><img src="<?php bloginfo('template_url'); ?>/images/post_word_icon.jpg" /></a>
        </div>
    </div>
    <?php } else {} ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM