Site logo

[Resolved] hook to include all sub-categories

Home Forums Support [Resolved] hook to include all sub-categories

Home Forums Support hook to include all sub-categories

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #2133203
    roadlink

    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

    #2133293
    David
    Staff
    Customer Support

    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.

    #2133376
    roadlink

    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″]’); ?>

    #2133562
    David
    Staff
    Customer Support

    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.

    #2133878
    roadlink

    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?

    #2133886
    roadlink

    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?

    #2133931
    Ying
    Staff
    Customer Support

    Hi there,

    The hook location will be for All Product Category Archives.

    #2133936
    roadlink

    good to hear, thanks

    #2133942
    Ying
    Staff
    Customer Support

    No problem 🙂

    #2146090
    roadlink

    thx

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.