clestcruz
¿Hay alguna manera de ocultar las pestañas personalizadas si no hay contenido presente en el cuadro de campo? Estoy implementando esto con un complemento de campos personalizados avanzados. Hasta ahora, la pestaña sigue presente incluso si no hay contenido colocado
Aquí está el código que he colocado en mi 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');
}
ACTUALIZAR
//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, tuve el mismo problema y encontré una forma mucho mejor de resolverlo. Solo agrego la pestaña cuando tiene contenido. Tal vez esto ayude a todos los demás a encontrar este hilo.
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');
}
-
no funciona en 2021, debe agregar $post global; y luego llamar a get_field(‘direcciones’,$post->ID). solo the_field(‘directions’) es una función de salida que no puede verificar con la cláusula if
– laboratorios nh
21 de febrero de 2021 a las 12:38
Lo más probable es que haya una mejor manera de hacer esto, pero lo he logrado en el pasado con lo siguiente:
if( get_field('directions') )
{
echo the_field('directions');
}
else
{
echo "<style>.direction_tab_tab { display:none !important; }</style>";
}
Esto imprimirá el contenido del campo “direcciones” si hay texto en él, si no, imprimirá el css y ocultará la pestaña.
-
Buen día, actualicé mi código en mi functions.php basado en la solución que me diste pero todavía muestra la pestaña de direcciones en la interfaz 🙁
– clestcruz
13 de noviembre de 2014 a las 2:12
-
@clestcruz Es posible que deba echar un vistazo a la clase a la que se llama la pestaña. Supuse que se llamaría
.direction_tab_tab
podría ser diferente para ti.– Howli
13 de noviembre de 2014 a las 9:17
-
no, la clase que mencionaste era exactamente la misma. Incluso coloqué li.direction_tab_tab
– clestcruz
13 de noviembre de 2014 a las 9:30
-
Es
<style>.direction_tab_tab { display:none !important; }</style>
siendo impreso en absoluto?– Howli
13 de noviembre de 2014 a las 9:36
-
Revisé mis estilos en línea, no parece hacer eco del estilo
– clestcruz
13 de noviembre de 2014 a las 9:41
Una mejor manera será
if( get_field('directions') ) {
$tabs['direction_tab'] = array(
'title' => __( 'Direction', 'woocommerce' ),
'priority' => 60,
'callback' => 'woo_new_direction_tab_content'
);
}
Eso ocultará la pestaña si no hay contenido en la pestaña.
El método más fácil es quitar la pestaña.
Puede hacer esto en función del contenido del campo de texto, que en caso de que esté vacío, solo use esto.
unset( $tabs['direction_tab'] ); // Remove the additional direction tab
Y listo 🙂
Puede usar get_field() para verificar si hay contenido disponible.
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');
}
r. r.
Este código me funciona en 2021. Solo agrega una pestaña personalizada cuando el campo deseado está lleno. Si el campo está vacío, oculta la pestaña sin dejar rastro.
//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>';
}
}
tipo de paz
Básicamente, usa el campo con el contenido de la pestaña para mostrar condicionalmente la pestaña. El código verifica si el campo está vacío, si lo está, desarma la pestaña para que no se muestre. Si el campo tiene contenido, lo devolvió. También ajusté el contenido de la pestaña con el condicional. Es decir, compruebe si hay contenido y, si lo hay, devuelva la pestaña. Esto es opcional, ya que incluso sin la verificación, la pestaña no se devolverá si no hay contenido en el campo.
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;
}
-
Básicamente, usa el campo con el contenido de la pestaña para mostrar condicionalmente la pestaña. El código verifica si el campo está vacío, si lo está, desarma la pestaña para que no se muestre. Si el campo tiene contenido, lo devolvió. También ajusté el contenido de la pestaña con el condicional. Es decir, compruebe si hay contenido y, si lo hay, devuelva la pestaña. Esto es opcional, ya que incluso sin la verificación, la pestaña no se devolverá si no hay contenido en el campo.
– David Proctosaurio
17 de enero de 2018 a las 10:08