Tagged: conditional code, value checking
- This topic has 4 replies, 2 voices, and was last updated 7 years, 1 month ago by
cosmocanuck.
-
AuthorPosts
-
-
December 10, 2014 at 6:02 pm #3032
cosmocanuckMemberHi! How do I confirm that a field even has a value before I show it on the front end? If it’s empty, I’d like to avoid running that code. For example, I’m currently attempting this:
// Grab the claims $productClaims = get_post_meta($post->ID, 'claims', false); if ( !empty($productClaims) ) { // Display the claims echo '<ul>'; piklist(get_stylesheet_directory() . '/product-claims-template', array('data' => $productClaims, 'loop' => 'data') ); echo '</ul>'; }But even if $productClaims is an empty array (confirmed with a print_r command), the empty() check doesn’t work, it still runs the code. This results in a single bullet from an empty “ul”.
This is how I did it earlier in the year on my first Piklist site, I know it’s not optimal code but it did work:
<?php if ($resortData [weather_conditions_url][0] > '') { ?> <p><strong><a class="external" href="<?php echo $resortData [weather_conditions_url][0];?>" target="_new" rel="bookmark">Weather Conditions</a></strong></p> <?php } ?>But now I’m dealing with an array, not a single value so I presume that also makes it different (as well as the fact that this syntax is not recommended).
Thanks!
adam -
December 10, 2014 at 10:52 pm #3038
SteveKeymaster@cosmocanuck– I need to see the output of $productClaims.
Please run this code and paste the output here:
$productClaims = get_post_meta($post->ID, 'claims', false); piklist::pre($productClaims);
-
December 11, 2014 at 10:42 am #3043
cosmocanuckMemberWell, this is the darnedest thing, but if I add your suggested two lines of code anywhere to my template, it kills all the HTML prior to the display of all my Piklist fields, resulting in an unformatted and incomplete page. See attached. The productClaims info isn’t even shown…
Here’s what I added (including a couple of other lines for context):
global $post; $productClaims = get_post_meta($post->ID, 'claims', false); piklist::pre($productClaims); echo '<p id="productSizeVariety">' . get_post_meta($post->ID, 'size_variety', true) . '</p>';If I use print_r instead, I at least get this:
Array ( [0] => High in Protein [1] => Very High in Calcium [2] => Low in Carbs [3] => High in Iron [4] => Source of Fibre )
Any idea what’s happening?
Attachments:
You must be logged in to view attached files. -
December 11, 2014 at 1:22 pm #3052
SteveKeymasterI think this should work:
!empty($productClaims[0]) -
December 17, 2014 at 2:20 pm #3114
cosmocanuckMemberThanks Steve, I’ve made a note of it. I actually realized that this particular add-more field was better set up as Tags… but I’m sure this will come in handy sometime soon…!
-
-
AuthorPosts
- The topic ‘Checking if a field/array has a value’ is closed to new replies.