Support » Fixing WordPress » Want to add a class to product category link if in current product category.

  • I am trying to add a class to a product category link, if in that category. Here’s the code I have so far.

    function show_parent_categories( $args = array() ) {
    
    $args = array(  
        'hierarchical' => 1,
        'show_option_none' => '',
        'hide_empty' => 0,
        'parent' => $category->term_id,
        'taxonomy' => 'product_cat'
    );
    
    $product_categories = get_terms( 'product_cat', $args );
    
    $count = count($product_categories);
     if ( $count > 0 ){
     echo "<div id='main-categories-wrapper'><ul id='main-categories'>";
     foreach ( $product_categories as $product_category ) {
       echo '<li><a href="' . get_term_link( $product_category ) . '">' . $product_category->name . ' </li>';
    
     }
     echo "</ul></div>";
    }
    }

    Someone else had suggested the following but it’s not working for me:

    function show_parent_categories( $args = array() ) {
    
    $args = array(  
        'hierarchical' => 1,
        'show_option_none' => '',
        'hide_empty' => 0,
        'parent' => $category->term_id,
        'taxonomy' => 'product_cat'
    );
    
    $product_categories = get_terms( 'product_cat', $args );
    
    $count = count($product_categories);
     if ( $count > 0 ){
     echo "<div id='main-categories-wrapper'><ul id='main-categories'>";
     foreach ( $product_categories as $product_category ) {
       echo '<li><a class="'.(is_category($product_category->slug))?'active':'').'" href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</li>';
    
     }
     echo "</ul></div>";
    }
    }
Viewing 9 replies - 1 through 9 (of 9 total)
  • You need to change is_category($product_category->slug) in suggested code to is_tax( 'product_cat', $product_category->slug ). The code above only works with category, not custom taxonomy.

    Thanks for the response Truong. I keep getting the following error.

    Parse error: syntax error, unexpected ‘)’, expecting ‘,’ or ‘;’

    Do you know why this would be?

    Ok so I got it to where there isn’t an error. Is it possible to have the class show when you’re in a subcategory of that category as well?

    You can write a function to check if you’re in a subterm of a term (I call it is term because this is custom taxonomy, not category).

    function is_in_sub_term_page( $parent_id ) {
    	if ( ! is_tax( 'product_cat' ) ) {
    		// If you are not in product cat page, return false.
    		return false;
    	}
    
    	// Get current term id.
    	$term_object = get_queried_object();
    	$term_id = $term_object->term_id;
    
    	// This function check if $term_id is child of $parent_id.
    	return term_is_ancestor_of( $parent_id, $term_id, 'product_cat' );
    }

    Then use above function like this:

    $active = is_tax( 'product_cat', $product_category->slug ) ? 'active' : '';
    $parent_active = is_in_sub_term_page( $product_category->term_id ) ? 'parent-active' : '';
    echo '<li><a class=" . $active . ' ' . $parent_active . " href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</li>';

    You can find used functions in the codex page.

    • This reply was modified 3 years, 1 month ago by truongwp.

    The echo line is giving an error. Is there something missing there?

    Can you show the error?

    Oh, I missed '. Please use below code:
    echo '<li><a class="' . $active . ' ' . $parent_active . '" href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</li>';

    I don’t get an error anymore, however the class is being added to all categories. I can show you: Website

    Here’s how I have that full block of code:

    
    function is_in_sub_term_page( $parent_id ) {
    	if ( ! is_tax( 'product_cat' ) ) {
    		// If you are not in product cat page, return false.
    		return false;
    	}
    
    	// Get current term id.
    	$term_object = get_queried_object();
    	$term_id = $term_object->term_id;
    
    	// This function check if $term_id is child of $parent_id.
    	return term_is_ancestor_of( $parent_id, $term_id, 'product_cat' );
    }
    
    function show_parent_categories( $args = array() ) {
    
    $args = array(  
        'hierarchical' => 1,
        'show_option_none' => '',
        'hide_empty' => 0,
        'parent' => $category->term_id,
        'taxonomy' => 'product_cat'
    );
    
    $product_categories = get_terms( 'product_cat', $args );
    $active = is_tax( 'product_cat', $product_category->slug ) ? 'active' : '';
    $parent_active = is_in_sub_term_page( $product_category->term_id ) ? 'parent-active' : '';
    
    $count = count($product_categories);
     if ( $count > 0 ){
     echo "<div id='main-categories-wrapper'><ul id='main-categories'>";
     foreach ( $product_categories as $product_category ) {  
    		echo '<li><a class="' . $active . ' ' . $parent_active . '" href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</li>';
     }
     echo "</ul></div>";
    }
    }

    You must place 2 lines $active = ... and $parent_active = ... in foreach block, before echo a term.

    • This reply was modified 3 years, 1 month ago by truongwp.
Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Want to add a class to product category link if in current product category.’ is closed to new replies.