Viewing 4 reply threads
  • Author
    Posts
    • #3032
      cosmocanuck
      Member

      Hi! 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

    • #3038
      Steve
      Keymaster

      @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);
      
    • #3043
      cosmocanuck
      Member

      Well, 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.
    • #3052
      Steve
      Keymaster

      I think this should work: !empty($productClaims[0])

    • #3114
      cosmocanuck
      Member

      Thanks 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…!

Viewing 4 reply threads
  • The topic ‘Checking if a field/array has a value’ is closed to new replies.