I've put together a custom filter sidebar for my WooCommerce Category page.
Some info about the sidebar:
Q Much like with what I've done with $product_cat, I would like to be able to identify within the loop IF the parent category on the sidebar is the parent of the child category page the user is currently on. With this in place, I can then add a class to the parent category li.
I've spent some time crawling the website in an attempt to find a solution but sadly not.
My full code below:
global $wp_query;
$cat_name = "product_cat";
$parent_cat_terms = get_terms($cat_name, array('parent' => 0, 'orderby' => 'slug', 'hide_empty' => true, 'exclude' => 122 ));
// IF Current declaration
global $wp_query;
$product_cat = $wp_query->query_vars['product_cat'];
echo '<ul class="cat-filters">';
foreach ($parent_cat_terms as $parent_term) {
if( $parent_term->count > 0 ):
echo '<li class="cat-parent';
if( $parent_term->slug == $product_cat ){
echo ' class="page-cat"';
}
echo '"><a href="' . get_term_link($parent_term) . '"';
if (strpos($parent_term->slug, $product_cat ) !== false) {
echo ' class="page-cat"';
}
echo'>' . $parent_term->name . '</a>';
$child_cat_terms = get_terms($cat_name, array('parent' => $parent_term->term_id, 'orderby' => 'slug', 'hide_empty' => true));
if ( !empty( $child_cat_terms ) && !is_wp_error( $child_cat_terms ) ){
echo '<ul class="cat-child">';
foreach ($child_cat_terms as $child_term) {
if( $child_term->count > 0 ):
echo '<li><a href="' . get_term_link($child_term) . '"';
if( $child_term->slug == $product_cat ){
echo ' class="page-cat"';
}
echo'>' . $child_term->name . '</a></li>';
endif;
}
echo "</ul>";
}
echo '</li>';
endif;
}
echo '</ul>';
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.