Is there a way I can hide the custom tabs if there is no content present in the field box. I am implementing this with advanced custom fields plugin. So far the tab is still present even if there is no content placed
Here is the code that I have placed in my functions.php
add_filter( 'woocommerce_product_tabs', 'woo_new_direction_tab' );
function woo_new_direction_tab( $tabs ) {
// Adds the new tab
$tabs['direction_tab'] = array(
'title' => __( 'Direction', 'woocommerce' ),
'priority' => 60,
'callback' => 'woo_new_direction_tab_content'
);
return $tabs;
}
function woo_new_direction_tab_content() {
// The new tab content
echo the_field('directions');
}
UPDATE
//Direction Tab
add_filter( 'woocommerce_product_tabs', 'woo_new_direction_tab' );
function woo_new_direction_tab( $tabs ) {
// Adds the new tab
$tabs['direction_tab'] = array(
'title' => __( 'Direction', 'woocommerce' ),
'priority' => 60,
'callback' => 'woo_new_direction_tab_content'
);
return $tabs;
}
function woo_new_direction_tab_content() {
if( get_field('directions') )
{
echo the_field('directions');
}
else
{
echo "<style>li.direction_tab_tab{ display:none !important; }</style>";
}
}
Hej, I had the same problem and found a much nicer way to solve this. I only add the tab when it has content. Maybe this helps everybody else finding this thread.
There is most likely a better way of doing this, but I’ve achieved this in the past with the following:
This will print the contents of the “directions” field if there is text in it, if not it will print the css and hide the tab.
A better way will be
That will hide the tab if there is no content in the tab
The easiest method is to remove the tab.
You can do this based on the content of text field, which in case is empty just use this.
And you are done 🙂
You can use get_field() to check if there is content available.
}
This code works for me in 2021. It only adds a custom tab when the desired field is full. If the field is empty, it hides the tab without a trace.
Basically you use the field with the tab content in to conditionally show the tab. The code checks if the field is empty, if it is then it unsets the tab so it doesn’t display. If the field has content it it returned. I’ve also adjusted the tab content with the conditional too. I.e. check if there is content and if there is then return the tab. This is optional as even without the check the tab won’t be returned if there is no content in the field.