skip to Main Content

I’m using code that needs to display specific content on different category pages.

My categories have the following structure:

Man

  • Cloth
    • Shorts
      • Shorts with pockets

But the code below, displays content only on the parent category (Man), and the 1st level subcategory (Cloth):

add_action( 'woocommerce_archive_description', 'add_slide_text',1 );
function add_slide_text() {
    $cat = get_queried_object();

    if ( is_product_category() ) {
        if ( is_product_category( 'man' ) ||  $cat->parent === 233 ) {
            echo 'Hello man';
        } elseif ( is_product_category( 'woman' ) ||  $cat->parent === 232 ) {
            echo 'Hello woman';
        } else {
            echo '';
        }
    }
}

How can I force it to display content on subcategories of a lower level? For example, in "Shorts" and "Shorts with pockets" (and possibly lower)?

Any help is appreciated.

2

Answers


  1. You can use the WordPress function cat_is_ancestor_of().

    Check out the docs here: https://developer.wordpress.org/reference/functions/cat_is_ancestor_of/

    Basically, you give it two things: what you think is the parent category and what you expect to be the child category.

    No matter how deep the hierarchy goes, it will return true.

    Here’s a quick example of how you can make it work:

    $current_category = get_queried_object();
    $man_category_id = get_cat_ID('Man');
    
    // Check if the current category or its ancestors include the "Man" category
    if (cat_is_ancestor_of($man_category_id, $current_category->term_id)) {
        echo "Display your content here";
    }
    

    Just a heads up, I haven’t tested this code, so feel free to adjust it to fit your situation.

    Login or Signup to reply.
  2. You can use get_term_children() WordPress function with WooCommerce product category custom taxonomy, to display a specific text from for each specific product category "man" and "woman" terms and their children as follows:

    add_action( 'woocommerce_archive_description', 'add_slide_text', 1 );
    function add_slide_text() {
        // Targeting WooCommerce product category archives
        if ( is_product_category() ) {
            $current_term = get_queried_object();
            $taxonomy     = $current_term->taxonomy;
    
            // For "man" term (and term ID 233)
            $term_man_id      = get_term_by('slug', 'man', $taxonomy)->term_id; // Get "man" term ID
            $children_man_ids = (array) get_term_children($term_man_id, $taxonomy); // Get children terms IDs
            $man_terms_ids    = array_merge( array(233, $term_man_id), $children_man_ids ); // Merge terms IDs in a unique array
    
            // For "woman" term (and term ID 232)
            $term_woman_id      = get_term_by('slug', 'woman', $taxonomy)->term_id; // Get "woman" term ID
            $children_woman_ids = (array) get_term_children($term_woman_id, $taxonomy); // Get children terms IDs
            $woman_terms_ids    = array_merge( array(232, $term_woman_id), $children_woman_ids ); // Merge terms IDs in a unique array
    
            // Conditional text display
            if ( in_array( $current_term->term_id, $man_terms_ids ) ) {
                _e('Hello man', 'woocommerce');
            } 
            elseif ( in_array( $current_term->term_id, $woman_terms_ids ) ) {
                _e('Hello woman', 'woocommerce');
            }
        }
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search