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 am working on a creating a directory style page template that uses custom meta fields in Wordpress that are used to generate up to 10 directory listings on a page.

The below code works very well for this purpose with one exception. If one of my directory pages only has 5 listings, the code is still displayed for listings 6-10 even if the values aren't present.

 <div class="listing"> 
 <h3 class="listing-title"><?php $values = get_post_custom_values("listing_heading_10"); 
 echo $values[0]; ?></h3> 
 <div class="listing-body">
 <p><?php $values = get_post_custom_values("listing_description_10"); echo $values[0]; ?>  </p> 
 </div><p class="listing-footer"><?php $values = get_post_custom_values("listing_url_10");    
 echo $values[0]; ?></p>  

What do I need to add so code is only displayed if the custom meta fields are populated for that instance.

In other words I am looking for a rule that says:

If values are present then display full directory listing (i.e. listing_heading_10, listing_description_10 & listing_url_10)

If none of these values are present then display nothing

Any help or advice on this would be greatly appreciated.

share|improve this question
    
I posted some PHP for this in a related thread: wordpress.stackexchange.com/questions/82383/… I'm working on some minor improvements, but it might help anyone who lands on this page. –  Jennifer Michelle Jan 20 '13 at 15:42

3 Answers 3

The easiest way would be to add a simple conditional:

$values = get_post_custom_values("listing_heading_10");

if ( $values ) { ?>
     <h3 class="listing-title"><?php echo $values[0]; ?></h3> 
<?php }

That said, is there any reason that you can't put your output code in a foreach loop?

e.g.

$values = get_post_custom_values( 'some_value' );

foreach ( $values as $value ) {
     // echo output content here
}

It may be helpful to put your custom meta values into an array, to facilitate looping through them if they exist. So, instead of:

$values = get_post_custom_values("listing_heading_10")

...you could have:

$values get_post_custom_values( 'listing_heading' )

And instead of

$values

...you could output:

$values[10]

etc.

share|improve this answer
2  
So, I don't mind the down-votes if the answer isn't correct, or is poorly written. But it would be incredibly helpful if such downvotes were accompanied by comments explaining the reasoning. I'd like to know how I can provide better answers/help to the people asking questions! –  Chip Bennett May 3 '11 at 13:29

So I tried using your above recommendation regarding the conditional (see code below) but I am still having the issue where if the meta box is not populated the code is still visible in the source if the page has less than 10 listings. Am I doing something wrong?

I also like the idea around outputting code in the foreach loop and using an array, but I have to be honest I am unfortunately very new to programming and not exactly sure how to write that.

Thanks again for your help, I really appreciate it.

 <div class="listing"> 

 <?php $values = get_post_custom_values("listing_heading_10"); 
 if ( $values ); ?>
 <h3 class="listing-title"><?php echo $values[0]; ?></h3>


 <?php $values = get_post_custom_values("listing_description_10"); 
 if ( $values ); ?>
 <div class="listing-body"><p><?php echo $values[0]; ?></p></div>

 <?php $values = get_post_custom_values("listing_url_10");
 if ( $values ); ?>
 <p class="listing-footer"><?php echo $values[0]; ?>
 </p>
 </div>
 <?php } 
share|improve this answer

If you only want the first value, there's no need to be fetching all the values just to extract one value from the array.

Custom Fields - Function Reference
You should use get_post_meta instead setting the third arg to true (for a single value).

eg.

get_post_meta( $post->ID, 'your-field-name', true );

You then not need to keep referencing the first item in the array, eg. $arr[0] .. nor would you need loops to iterate data you only want a single value from.

get_post_meta returns an empty array/string when there's no data(string/array depends whether you're fetching multiple or single - ie. what you set for the third param), but you could also potentially have an empty value for the field, so this should cover the field being set but empty, or not being present all together.

$field = get_post_meta( $post->ID, 'your-field-name', true );
if( !empty( $field ) ) {
    // Do whatever you like when the field has a value..
}
share|improve this answer

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.