如果字段框中没有内容,有没有办法隐藏自定义选项卡。我正在用高级自定义字段插件实现这一点。到目前为止,即使没有内容放置,标签仍然存在
这是我放在函数中的代码。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');
}
更新
//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,我遇到了同样的问题,并找到了一种更好的方法来解决这个问题。我只在选项卡有内容时才添加它。也许这有助于其他人找到这个线索。
add_filter( 'woocommerce_product_tabs', 'woo_new_direction_tab' );
function woo_new_direction_tab( $tabs ) {
// Adds the new tab
if (!empty(the_field('directions'))) {
$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');
}
很可能有更好的方法可以做到这一点,但我过去已经通过以下方法实现了这一点:
if( get_field('directions') )
{
echo the_field('directions');
}
else
{
echo "<style>.direction_tab_tab { display:none !important; }</style>";
}
如果"directions"字段中有文本,它将打印该字段的内容,如果没有文本,则打印css并隐藏选项卡。
更好的方法是
if( get_field('directions') ) {
$tabs['direction_tab'] = array(
'title' => __( 'Direction', 'woocommerce' ),
'priority' => 60,
'callback' => 'woo_new_direction_tab_content'
);
}
如果选项卡
最简单的方法是删除选项卡。
您可以根据文本字段的内容来执行此操作,如果文本字段为空,只需使用此即可。
unset( $tabs['direction_tab'] ); // Remove the additional direction tab
您完成了:)
您可以使用get_field()来检查是否有可用的内容。
add_filter( 'woocommerce_product_tabs', 'woo_new_direction_tab' );
function woo_new_direction_tab( $tabs ) {
// Check if there is content and add the tab if so
if(get_field(direction_tab)){
$tabs['direction_tab'] = array(
'title' => __( 'Direction', 'woocommerce' ),
'priority' => 60,
'callback' => 'woo_new_direction_tab_content'
);
}
return $tabs;
}
function woo_new_direction_tab_content() {
echo get_field('directions');
}
这段代码在2021年对我有效。它只在所需字段已满时添加一个自定义选项卡。如果该字段为空,则会隐藏该选项卡而不进行跟踪。
//Add a custom product data tab
add_filter( 'woocommerce_product_tabs', 'woo_new_custom_tab' );
function woo_new_custom_tab( $tabs ) {
global $post, $product;
// Adds the new tab
if( get_field('field_123') ) {
$tabs['custom_tab'] = array(
'title' => __( 'Custom Tab', 'woocommerce' ),
'priority' => 25,
'callback' => 'woo_new_custom_tab_content'
);
}
return $tabs;
}
//Add content to a tab and hide it if it is empty
function woo_new_custom_tab_content() {
global $post, $product;
if( get_field('field_123') ) {
echo '<h2>Custom Tab</h2>';
echo '<p>'.get_field('field_123',$post->ID).'</p>';
}
}
基本上,您使用中包含选项卡内容的字段来有条件地显示选项卡。代码检查字段是否为空,如果为空,则取消设置选项卡,使其不显示。如果字段包含内容,则返回。我还用条件调整了选项卡的内容。即,检查是否有内容,如果有,则返回选项卡。这是可选的,因为即使没有检查,如果字段中没有内容,也不会返回选项卡。
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'
);
if(empty(get_field('directions'))):
unset( $tabs['direction_tab'] );
else:
return $tabs;
endif;
}
function woo_new_direction_tab_content() {
// The new tab content
if(get_field('directions')):
echo '<h2>Directions</h2>';
the_field('directions');
endif;
}