Site logo

Archived topic

display rules - location - post category

3 replies · Started by kiant123 on August 30, 2022

Viewing posts 1–4 of 4

Hi,

I've added a hook affiliate disclaimer that I want to appear on any post with the category 'products' that I have. I've set this up and see that it doesn't work on the top-level category tag. For instance, I have a top level category 'products' then underneath I have numerous nested categories, and I only see the hook appear when i put it on the bottom level nested category. Is there a way to adjust the display rules so that it will display on all categories under the main product category or do i have to put the display rule for each nested category underneath it?

I'm not sure if this makes sense but i've added some screenshots to help illustrate this.

Thanks

Hi there,

so we want to check if the current post has a category term that is a child of products.
Currently you would either need to:

a. Set each of the Post Category -> Child terms in the Element Display Rules

OR

b. Use the generate_element_display filter hook to set element display to True if the posts are in child term

add_filter( 'generate_element_display', function( $display, $element_id ) {
    /// Change 123 below to the ID of the Element we're targeting.
    if ( 123 === (int) $element_id ) {
        $args = array(
            'include'  => get_the_ID(),
            'category' => 456, // Change 456 to the parent category ID we're checking for children in.
            'fields'   => 'ids',
        );

        if ( 0 < count( get_posts( $args ) ) ) {
            $display = true;
        }
    }

    return $display;
}, 10, 2 );

Note the two IDs that need changing

Hi David,

I decided to go with the first option and just fill all the categories one by one in the rules.

Thanks for your help!

You're welcome

This archived topic is closed to new replies.