Home › Toolset Professional Support › [Resolved] If a custom field is empty for ALL items in a view, hide whole table column
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Tagged: Views, Views plugin
This topic contains 1 reply, has 1 voice.
Last updated by BD 6 years, 7 months ago.
The table in the screenshot is dynamically generated using a view. The THRUST column fields are all empty for this set of results. If this is the case, I would like to conditionally hide the whole empty column (including column heading). However, if even one result contains a thrust value, the whole column should be shown.
So I need the view to check the content of a specific custom field for ALL results - and if ALL are empty, then hide the whole column (and column header) that contains that custom field.
Please can you help? Thanks, Nick
OK - I figured it out with the help of:
https://toolset.com/forums/topic/count-of-custom-field-if-it-has-an-entry/
I had to create several shortcodes (suffixed with a, b, c etc so that I could target multiple custom fields):
global $total_a; function add_total_shortcode_a($atts, $content = '') { global $total_a; $total_a += wpv_do_shortcode($content); } add_shortcode('add-to-total-a', 'add_total_shortcode_a'); function show_total_shortcode_a() { global $total_a; $totalNew_a = $total_a; $total_a = 0; return $totalNew_a; } add_shortcode('show-total-a', 'show_total_shortcode_a'); global $total_b; function add_total_shortcode_b($atts, $content = '') { global $total_b; $total_b += wpv_do_shortcode($content); } add_shortcode('add-to-total-b', 'add_total_shortcode_b'); function show_total_shortcode_b() { global $total_b; $totalNew_b = $total_b; $total_b = 0; return $totalNew_b; } add_shortcode('show-total-b', 'show_total_shortcode_b');
I then created a conditionally rendered class: hidecell - set outside the loop:
[wpv-layout-start] [wpv-items-found] <table id="productlist" width="100%" border="0" cellspacing="0" cellpadding="0" class="text-center"> <tr> <th>Column Heading One</th> <th>Column Heading Two</th> <th>Column Heading Three</th> <th class="hidecella">Thrust (Kgf)</th> <th class="hidecellb">Thrust (Lbs)</th> <th>Column Heading Five</th> <th>Column Heading Six</th> </tr> <!-- wpv-loop-start --> <wpv-loop> [add-to-total-a] [types field="thrust-kgf" output="raw"][/types] [/add-to-total-a] [add-to-total-b] [types field="thrust-lbf" output="raw"][/types] [/add-to-total-b] <tr> <td><a data-open="[wpv-post-slug]">[types field='part-number' output='raw'][/types]</a></td> <td>[types field='max-power-kw' output='raw'][/types]</td> <td>[types field='max-power-hp' output='raw'][/types]</td> <td class="hidecella">[types field='thrust-kgf' output='raw'][/types]</td> <td class="hidecellb>[types field='thrust-lbf' output='raw'][/types]</td> <td>[types field='prop-diameter-mm' output='raw'][/types]</td> <td>[types field='prop-diameter-inches' output='raw'][/types]</td> </tr> </wpv-loop> <!-- wpv-loop-end --> </table> [wpv-view name="list-of-thrusters-products-nested-reveals"] [wpv-conditional if="( '[show-total-a]' eq '' ) OR ( '[show-total-a]' eq '0' )"]<style>.hidecella {display:none;}</style>[/wpv-conditional] [wpv-conditional if="( '[show-total-b]' eq '' ) OR ( '[show-total-b]' eq '0' )"]<style>.hidecellb {display:none;}</style>[/wpv-conditional] [/wpv-items-found] [wpv-no-items-found] [/wpv-no-items-found] [wpv-layout-end]