Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm building a single template right now for a custom post type. I'm wondering if there is a way to check if either at least one or multiple of the custom values has been entered so I can display the div accordingly. Basically, I want to display the div it has anything filled out, if it is left blank then don't show it. Is this possible?

Here is a sample snippet I have:

<?php IF STAEMENT HERE ?>
   <div id="downloads">
        <ul>
        <?php if(get_post_meta($post->ID, 'sermon_video', true)) { ?>
            <li><a href=" <?php echo $post_meta_data['sermon_video'][0]; ?>">Video</a></li>
        <?php } ?>
        <?php if(get_post_meta($post->ID, 'sermon_audio', true)) { ?>
            <li><a href=" <?php echo $post_meta_data['sermon_audio'][0]; ?>">Audio</a></li>
        <?php } ?>
        <?php if(get_post_meta($post->ID, 'sermon_document', true)) { ?>
            <li><a href=" <?php echo $post_meta_data['sermon_document'][0]; ?>">PDF</a></li>
        <?php } ?>
        </ul>
   </div>
<?php ENDIF ?>
share|improve this question

2 Answers 2

up vote 1 down vote accepted

Since you already using get_post_custom a slightly cleaner yet still dirty (php) way of doing it.

$sermondata = get_post_custom($post->ID);

if( isset($sermondata['sermon_audio']) || isset($sermondata['sermon_video']) || isset($sermondata['sermon_document']))
{ ?>
<div id="downloads">
// rest of your echo output
</div>
<?php } ?>

ps . Just make sure the value is not null, to be safe you can combine the check with array_key_exists() (makes it slower though).

share|improve this answer
    
Thanks, this seems to work perfect for what I was trying to do. I appreciate it. –  ftntravis Dec 1 '12 at 3:38

Get your values first.

<?php 
$sermondata['sermon_video'] = get_post_meta($post->ID, 'sermon_video', true);
$sermondata['sermon_audio'] = get_post_meta($post->ID, 'sermon_audio', true);
$sermondata['sermon_document'] = get_post_meta($post->ID, 'sermon_document', true);

if (!empty($sermondata)) { ?>
   <div id="downloads">
      <ul>
      <?php if(isset($sermondata['sermon_video'])) { ?>
        <li><a href=" <?php echo $post_meta_data['sermon_video'][0]; ?>">Video</a></li>
      <?php }
      if(isset($sermondata['sermon_audio'])) { ?>
        <li><a href=" <?php echo $post_meta_data['sermon_audio'][0]; ?>">Audio</a></li>
      <?php } 
      if(isset($sermondata['sermon_document'])) { ?>
        <li><a href=" <?php echo $post_meta_data['sermon_document'][0]; ?>">PDF</a></li>
      <?php } ?>
      </ul>
   </div><php 
} ?>

If I knew more about what what $sermondata looks like it might be possible to simplify that code more.

share|improve this answer
    
I use this to grab all of the custom data: $post_meta_data = get_post_custom('post_type=sermons'); Is there anything else you would need to know? Thanks for the reply I will try it out. –  ftntravis Dec 1 '12 at 0:50
    
For this example I gave, they are all text fields that are being filled out and displayed on the front end. –  ftntravis Dec 1 '12 at 0:52
    
get_post_customs only parameter should be a post ID, not a query-string-like string. What does var_dump(get_post_custom($post->ID)) look like? –  s_ha_dum Dec 1 '12 at 0:56
    
Never noticed that, thanks for the info. It does the same thing, I switched it over to the proper way. Any idea now how I can clean the above code up? –  ftntravis Dec 1 '12 at 1:06
    
It does the same thing because it basically defaults to 'this post'. It wouldn't work correctly outside of a Loop. I wouldn't worry about it being 'bad' code. It isn't. You might be able to do it fewer lines depending on what your meta data looks like. –  s_ha_dum Dec 1 '12 at 1:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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