Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
Cómo excluir una categoría del widget de lista de categorías para usuarios que no son administradores

Estoy tratando de excluir one category del widget de lista de categorías en las páginas de product archive de la Shop y del producto para users other than the administrator . Pero por alguna razón no funciona.

Cualquier ayuda, gracias.

 function exclude_category( $terms, $taxonomies, $args ) { $new_terms = array(); /*if ( !is_admin() && ( is_product() || is_shop() || is_product_category() || is_product_tag() ) ){ */ if ( !is_admin() || is_product() || is_shop() || is_product_category() || is_product_tag() ){ foreach ( $terms as $key => $term ) { if( is_object ( $term ) ) { if ( 'some-category' == $term->slug && $term->taxonomy = 'product_cat' ) { unset($terms[$key]); } } } } return $terms; } add_filter( 'get_terms', 'exclude_category', 10, 3 );

Me inspiré en esto: https://stackoverflow.com/a/48849931/16275280

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Exclusión de una categoría de producto en WooCommerce

Utilice las siguientes comprobaciones condicionales en su lugar:

  • Para verificar si un usuario es administrador o no, puede usar la propiedad de roles del objeto de usuario devuelto por la función wp_get_current_user Docs .
  • Para verificar si está en páginas de woocommerce o no (es decir is_product() || is_shop() || is_product_category() || is_product_tag() ), simplemente puede usar esta función is_woocommerce() .

Así que todo el código sería:

 add_filter('get_terms', 'exclude_category', 10, 3); function exclude_category($terms, $taxonomies, $args) { $roles = wp_get_current_user()->roles; if ( !in_array('administrator', $roles) && is_woocommerce() ) { foreach ($terms as $key => $term) { if (is_object($term)) { if ('decor' == $term->slug && $term->taxonomy == 'product_cat') { unset($terms[$key]); } } } } return $terms; }

Nota:

  • He usado 'decor' como el slug de categoría para probar este fragmento en mi sitio, así que no olvides reemplazarlo con tu propio slug de categoría.

Y aquí está el resultado:

ingrese la descripción de la imagen aquí


Exclusión de una categoría en Wordpress

 add_filter('get_terms', 'exclude_category', 10, 3); function exclude_category($terms, $taxonomies, $args) { $roles = wp_get_current_user()->roles; if ( !in_array('administrator', $roles) ) { foreach ($terms as $key => $term) { if (is_object($term)) { if ('uncategorized' == $term->slug && $term->taxonomy == 'category') { unset($terms[$key]); } } } } return $terms; }

Nota:

  • He usado 'uncategorized' como el slug de categoría para probar este fragmento en mi sitio, así que no olvides reemplazarlo con tu propio slug de categoría.

Excluyendo múltiples categorías en WooCommerce

Puede configurar una matriz y usar la función in_array para la verificación condicional. Me gusta esto:

 $excluded_cat_slugs = array ('hoodies', 'decor'); if(in_array($term->slug, $excluded_cat_slugs) && $term->taxonomy == 'product_cat')

Excluyendo múltiples categorías en Wordpress

 $excluded_cat_slugs = array ('uncategorized', 'test-category'); if(in_array($term->slug, $excluded_cat_slugs) && $term->taxonomy == 'category')
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!