Archived topic
hook to include all sub-categories
9 replies · Started by roadlink on February 25, 2022
Hello,
I need to add a banner at the top of a number of WooCommerce category pages – I have created a new Element and the hook is set as woocommerce_before_main_content and the location as, for example, computers – this works great!
But – under the Computers category there are a number of sub-categories – eg: Notebooks, All-in-One PC etc – is there an option for having all sub-categories included in the location.
I can add them one by one but when I add new sub-category, I have to remember to add it here.
Which is very hard to remember
Many thanks
Hi there,
there isn't a display option ( yet ) for sub-categories or child_of. It is something we're looking into.
Its possible to use the generate_element_display filter to set custom display conditions:
https://docs.generatepress.com/article/generate_element_display/
However, it would mean you have to 'hard code' the parent category terms in the filter, so if you added new parents then you have to edit the code.
Let me know if thats an option and i can see if we can provide a snippet for that.
Hi David,
I have 5 different parent category.
I plan to have different sliders for each parent categories and sub-categories under them.
If it is possible, would be great to have snippet for one example.
I plan to add this shortcode. It works with or without php.
<?php echo do_shortcode('[metaslider id="158760"]'); ?>
You could do this in one Hook:
<?php
$cat = get_queried_object();
if ( $cat->term_id === 16 || $cat->parent === 16 ) {
echo do_shortcode('[metaslider id="158760"]');
}
if ( $cat->term_id === 30 || $cat->parent === 30 ) {
echo do_shortcode('[metaslider id="4569123"]');
}
// Add more if conditions if required.
if ( $cat->term_id === 40 || $cat->parent === 40 ) {
echo do_shortcode('[metaslider id="12321312"]');
}
?>
The hook location will be for All Product Category Archives.
Each condition in that code eg.
if ( $cat->term_id === 40 || $cat->parent === 40 ) {
references the Category Term ID - in the example that is 40 you need to update both instances to match the category you want to display the shortcode on.
Hi David,
That looks perfect. I am not very sure how to add it.
Element and hook or just to put it in my functions.php?
I have added hook element. https://prnt.sc/2pbhcuGBUulE
Php and shortcode activated.
Location entire site
It works. Is this the right way to do it?
Hi there,
The hook location will be for All Product Category Archives.
good to hear, thanks
No problem :)
thx