i have a private page that has a series of custom fields. what i’d like to do is be able to discover how many of these fields are filled and then populate a sidebar based on the contents of the fields. i have a block of code that works, but it always looks for those four fields specifically and if there’s nothing in there, it’s just displays an empty block.
here’s the code i’m wanting to condense and make smarter:
<li class="sponsorSlot">
<a href="<?php echo get_post_meta(158, 'sponsor_01_uri', true); ?>" title="<?php echo get_post_meta(158, 'sponsor_01_uriTitle', true); ?>">
<img src="<?php bloginfo('stylesheet_directory'); ?>/<?php echo get_post_meta(158, 'sponsor_01_image', true); ?>">
</a>
</li>
<li class="sponsorSlot">
<a href="<?php echo get_post_meta(158, 'sponsor_02_uri', true); ?>" title="<?php echo get_post_meta(158, 'sponsor_02_uriTitle', true); ?>">
<img src="<?php bloginfo('stylesheet_directory'); ?>/<?php echo get_post_meta(158, 'sponsor_02_image', true); ?>">
</a>
</li>
<li class="sponsorSlot">
<a href="<?php echo get_post_meta(158, 'sponsor_03_uri', true); ?>" title="<?php echo get_post_meta(158, 'sponsor_03_uriTitle', true); ?>">
<img src="<?php bloginfo('stylesheet_directory'); ?>/<?php echo get_post_meta(158, 'sponsor_03_image', true); ?>">
</a>
</li>
<li class="sponsorSlot">
<a href="<?php echo get_post_meta(158, 'sponsor_04_uri', true); ?>" title="<?php echo get_post_meta(158, 'sponsor_04_uriTitle', true); ?>">
<img src="<?php bloginfo('stylesheet_directory'); ?>/<?php echo get_post_meta(158, 'sponsor_04_image', true); ?>">
</a>
</li>
you’ll see what i’m trying to do. it would be nice if it didn’t matter if there were 1 or 8 entries, it would populate what was there without having to explicitly specify. my thoughts were to build an array and then just step through the array to build the elements i need.
thanks all!
WR!
you can just add an if condition to check the existence of the custom field .
Something like
yeah you can loop through the values returned by get_post_meta(), but as per documentation by default it returns an array of all values of the key you specify, but you are overriding it by specifying
$single
toFALSE
.