X

Cómo mostrar u ocultar widgets en páginas específicas en WordPress

Snippets by IsItWP

¿Estás buscando una manera de mostrar u ocultar widgets en páginas específicas de WordPress? Si bien es probable que haya un plugin para esto, hemos creado un fragmento de código rápido que puede utilizar para mostrar u ocultar widgets en páginas específicas en WordPress.

Lo primero que debe hacer es obtener el nombre ID del widget que desea definir. Puede hacer esto viendo la fuente de la página o usando una herramienta de inspección web para ver el ID del widget. El ejemplo actual está configurado para mostrar el widget ‘pages’ sólo en la página de contacto. No olvides que is_page también acepta un array de nombres de páginas e IDs.

Instrucciones:

Todo lo que tienes que hacer es añadir este código al archivo functions.php de tu tema o en un plugin específico del sitio:

add_filter( 'widget_display_callback', 'hide_widget_pages', 10, 3 );
function hide_widget_pages( $instance, $widget, $args ) {
  if ( $widget->id_base == 'pages' ) { // change 'pages' to widget name
     if ( !is_page( 'contact' ) ) {    // change page name
         return false;
     }
  }
}

Nota: Si es la primera vez que añades fragmentos de código en WordPress, consulta nuestra guía sobre cómo añadir correctamente fragmentos de código en WordPress, para no romper accidentalmente tu sitio.

Si le gustó este fragmento de código, por favor considere revisar nuestros otros artículos en el sitio como: 30 maneras de ganar dinero con los blogs en línea y cómo crear un formulario de WordPress con un selector de fecha.

Comentarios   Deja una respuesta

  1. love this tutorial snippet, thank you!

    unfortunately, i got 2 warnings:
    array_merge(): Argument #2 is not an array
    array_merge(): Argument #1 is not an array

    it did remove the sidebars from conditional page however, even w errors.

    i’m still learning PHP and not sure how to include array $args, if you could please advise… that would be wonderful.

    also wondering if there’s a way to remove 1(one) or more specific widget(s), from specified sidebar without removing all.

    either way, thanks for posting this snippet <3

    1. Are you adding an array to the snippet?

  2. Hey 🙂
    Your code worked for me on all the wordpress pages, but this code didn’t work for me on the WooCommerce store page. I have a basic product search bar widget in the header on every page, but I only want it to be on the STORE page. My woocommerce store page id is 552, so I used this code:

    add_filter( ‘widget_display_callback’, ‘hide_widget_pages’, 10, 3 );
    function hide_widget_pages( $instance, $widget, $args ) {
    if ( $widget->id_base == ‘woocommerce_product_search’ ) { // change ‘pages’ to widget name
    if ( !is_page( ‘552’ ) ) { // change page name
    return false;
    }
    }
    }

    When I add another page id like the About Us page id is 551 …. if I set this id, then your code will hide the product search bar on every site except the About Us page. But when I type the id of the STORE page which is 552, then the search bar is missing from all the pages, also missing from the store page of woocommerce. Can you help me please? Many thanks 🙂

    1. You may want to double check that 552 is the correct id. If it is, you may want to reach out to WooCommerce support.

  3. Could you let e know how to change the snippet so that I can hide several widgets on specific pages?

  4. Awesome Joel! I’ll be looking forward to seeing more of what you’ve been working on.

  5. Very useful code. Thanks for sharing.

    1. No problem Sajan, Joel Worsham is going be contributing to wpsnipp.com so I’m sure he will have lots of great snippets to come.

  6. I like it. Thanks for sharing.

    Saves installing another plugin.

    1. Very true brad, nice and simple snippet!

Añadir un comentario

Nos alegra que haya decidido dejar un comentario. Tenga en cuenta que todos los comentarios se moderan de acuerdo con nuestra política de privacidad , y que todos los enlaces son nofollow. NO utilice palabras clave en el campo del nombre. Tengamos una conversación personal y significativa.

WordPress Launch Checklist

La lista definitiva para lanzar WordPress

Hemos recopilado todos los elementos esenciales de la lista de comprobación para el lanzamiento de su próximo sitio web de WordPress en un práctico ebook.
Sí, envíeme el ¡gratuito!