0

I've setup standard custom fields for some products in WooCommerce which are being displayed on each product page on the front end. However, I'd like to hide those fields that don't have a value entered. I've tried some suggestions made elsewhere on this site but they didn't work for me.

This is what I have on the description.php

<div id="customfield-meta">
<?php
// Display Custom Field Value
echo "<ul>";
echo "<li>Catalogue Number: ".get_post_meta( $post->ID, 'catalogue', true )."</li>";
echo "<li>License Type: ".get_post_meta( $post->ID, 'license', true )."</li>";
echo "<li>Width (px): ".get_post_meta( $post->ID, 'iswidth', true )."</li>";
echo "<li>Height (px): ".get_post_meta( $post->ID, 'isheight', true )."</li>";
echo "</ul>";
?>
</div> <!-- end #customfield-meta -->

Any ideas how I could do this? Thank you

0

You can use isset() function to check if it has a value.

In your case you can do like this :

    $width = get_post_meta( $post->ID, 'iswidth', true );
    if( isset( $width ) ){
      echo "<li>Width (px): ".$width."</li>";
    }
  • Thanks for the reply. I think I must be doing something wrong because it didn't work. I tried replacing this line with your suggestion - echo "<li>Width (px): ".get_post_meta( $post->ID, 'iswidth', true )."</li>"; – kamala May 27 '15 at 8:29
  • 1
    Try ! empty( $width ) instead. @kamala – Nicolai May 27 '15 at 8:38
  • var_dump( $width ); //To check the value – mishu May 27 '15 at 10:10
  • Thanks ialocin, the ! empty works. I just have one other custom field that I can't get right. It pulls a link from the field entry so users can download a file. I tried this code but it doesn't work; $comp = get_post_meta( $post->ID, 'comp', true ); if( ! empty( $comp ) ){ echo "<li><a href="' . get_post_meta( $post->ID, 'comp', true ) . '">Download Video Comp</a> (right-click/save link or target as) ".$comp."</li>"; – kamala May 27 '15 at 11:53
  • A simple if( $width ) works too ;) – Mayeenul Islam May 29 '15 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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