This code works with categories but not with child sub-categories.

How can I make it work a child category that is not in the url (slug)?

global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;

if ( in_array( 'lucha', $categories ) ) {
  echo '<div class="nota-categorias-especiales">Important: this product has special conditions.

<button class="single_add_to_cart_button button alt" type="submit">Contact Us</button>

 </div>  ';

  } else {}
  • 1
    I have removed the woocommerce tag and the name from the title as this is a general wordpress question and the woocommerce tag is irrelevant. Woocommerce questions are off topic here btw :-). Please feel free to read How to Ask and take a tour to see how the site operates – Pieter Goosen Oct 27 '14 at 9:10

You can probably just make use of has_term( $term, $taxonomy, $post ) which will check if the specific post belongs to the term given

You can do something like this (Edit: Add post ID if this is used in a function)

global $post;
if(has_term( 'lucha', 'product_cat', $post->ID )) {
    //do something if post has lucha term
    //do something else if the post don't have lucha term
  • hi pieter I will try it, should I remove the previous lines? I have tried with this code but breaks the page... – dmtnexer Oct 27 '14 at 9:08
  • You can remove the code that you have. Just remember, you are going to mix php with html here, and I suspect that is why you get that white page. Remember to use opening and closing php tags when moving to and from php and html – Pieter Goosen Oct 27 '14 at 9:14
  • thanks for yout help pieter, I have tried: with this code but still gives a blank page.... i share: – dmtnexer Oct 27 '14 at 9:36
  • <?php if(has_term( 'lucha', 'product_cat' )) { echo '<div class="nota-categorias-especiales">Importante: para comprar productos de tipo Sustratos debes usar un formulario de Pedidos Especial. <button class="single_add_to_cart_button button alt" type="submit">Hacer un pedido</button> </div> '; } else {} ?> – dmtnexer Oct 27 '14 at 9:38
  • Turn debug on. You have a syntax error somewhere. – Pieter Goosen Oct 27 '14 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.