Conditional Metabox Display
-
Hello Sir,
I just want to help if you can help me with a Code that can help me display CMB2 Metabox Conditionally on my website.The Conditional Display is this:
1.) Show those Custom Fields according to Category. Meaning that, show:
a.) ART SIZE and PRODUCT YEAR if Category is equal to Artworks.
b.) CRT PRICE and PRODUCT YEAR if Category is Equal to Bags.
c.) CRAFT SIZE, MATERIAL and WEIGHT if Category is Equal to Crafts.
2.) Hide Custom Fields if value is equal to Null. Meaning that, if these fields are not filled in, with a value, they should not appear.
Now, this is the Code that I used to display the Metabox Information on my WooCommerce Single Product Page:
// DISPLAY CUSTOM FIELDS IN WOOCOMMERCE SINGLE PRODUCT PAGE add_action( 'woocommerce_single_product_summary', 'wpsh_single_posts_custom_meta_fields', 10 ); function wpsh_single_posts_custom_meta_fields(){ $post_id = get_the_ID(); $post = get_post( $post_id ); // Replace $some_number and some_number with your own field ID-s // For example: if your field ID is my_field, then replace $some_number with $my_field and some_number with my_field $month_year = get_post_meta( $post->ID, 'month_year' ); // Date field $art_size = get_post_meta( $post->ID, 'art_size' ); // Text field // Replace $some_number and some_number with your own field ID-s. NB! Don’t remove [0]. // For example: if your field ID is my_field, then replace $some_number with $my_field and $some_number[0] with $my_field[0] // Date field if(!empty($month_year)){ echo '<p class="creation-year"><span style="font-weight: bold; color: #4cbb17;">PRODUCT YEAR</span>: ' . $month_year[0] . '</p>'; } // Text field if(!empty($art_size)){ echo '<p class="art-dimension"> <span style="font-weight: bold; color: #4cbb17;">ART SIZE</span>: ' . $art_size[0] . '</p>'; } echo '</p>'; }
Can you Please assist me with modifying my code to add the Conditional Statement–so that CMB2 can display my Metaboxes according to Category?
Needing to hear from you soon.
Regards.
The page I need help with: [log in to see the link]
- The topic ‘Conditional Metabox Display’ is closed to new replies.