PHP If/Else for Wordpress Custom Fields

Posted by HollerTrain on Stack Overflow See other posts from Stack Overflow or by HollerTrain
Published on 2011-01-03T20:03:31Z Indexed on 2011/01/03 20:54 UTC
Read the original article Hit count: 170

Filed under:
|

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 } ?>  

© Stack Overflow or respective owner

Related posts about php

Related posts about Wordpress