Site logo

Archived topic

GP Hook on certain child pages

8 replies · Started by Ashley on November 7, 2019

Viewing posts 1–9 of 9

Hi There,

I'm trying to display a hook (ID = 18275)
only on child pages of a certain page (ID = 98).

At the moment I can only get it to display on child pages AND the parent page so I'm obviously doing something wrong!

Hi there,

How are you currently displaying it on child and parent pages?

If you want, we can filter the hook to display on all child pages of a specific parent:

add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
    global $post;

    if ( 123 === $element_id && ( is_page() && 456 == $post->post_parent ) ) {
        $display = true;
    }

    return $display;
}, 10, 2 );

In the above, you just need to replace 123 with the ID of the hook you're targeting, and 456 with the ID of the parent page.

Hi Tom,

Thanks for this, I've added this to functions.php it was slightly different from the snippet I'd got from the knowledgebase.

It's still appearing on all inner pages, however.

Here's the display settings on the element itself, do these have to be different?

Screenshot

screenshot

Ok so, i've change the display rule to just display on the parent page and that seems to have stopped it showing on other pages.

So now it's displaying on the parent page (training courses) and child pages of that parent.

I just need to exclude the training course parent page.

[SOLVED]

So, just documenting this for anyone that stumbles across this forum post.

I set the display rule to one of the child pages (a random one) and set an exclusion rule to exclude the parent page.

I added Tom's code from above to functions.php and this now displays the element on all child pages.

It seems as though the function doesn't override the element display rule but runs alongside it.

Glad you got it working - thanks for letting us know! :)

Is there any chance this could be a feature?

Having a display rule that allows us to set "Page" and "Child of" would be rather useful for anyone wanting to have the same header on a group of pages or color theming different areas of a site.

Agreed, this would be nice. Definitely something I'll look at adding :)

This archived topic is closed to new replies.