[Resolved] Display Rule for child pages

Home Forums Support [Resolved] Display Rule for child pages

Home Forums Support Display Rule for child pages

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1115795
    Regina

    Hi there,

    I created a hook with an arrow linked to the parent page, which I want to display on all child pages of one parent page, but not on the parent page itself. I miss the exact display rule like “all child pages” in combination with “parent page”. Is it possible to create my own display rule?

    I found the following – do I have to create two hooks and one with something like this?:

    ***
    Another useful example is to set the hook to display on the parent page and all the child pages automatically:

    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
        global $post;
    
        if ( 1180 === $element_id && ( is_page() && $post->post_parent == '415' ) ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );

    You need to replace 1180 with the ID of your Element, and 415 with the ID of the parent page.
    ***

    Thank you so much in advance!

    #1116184
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    That one filter should do exactly what you’re looking for.

    All you need to do is:

    1. Create the Hook, and note the Hook ID (it’s in the URL when you save your hook).
    2. Replace 1180 in the filter with the ID.
    3. Replace 415 in the filter with the ID of the parent page you’re targeting.

    That should be it.

    We’re planning on adding a Child Page option in the Display Rules soon 🙂

    #1116214
    Regina

    Hi Tom,

    and how do I find out the ID of my Element? Do I allocate it in the code of my element myself?

    I choosed custom hook and also checked PHP in the hook withe the code. Is a location rule necessary? (Actually I have it in the Element hook.)

    Do not plan too long to add this Child Page option in the Display Rules 😉

    Thanks again!

    #1116243
    Regina

    Sorry, you already wrote, that the ID is in the URL.
    it´s …/post.php?post=1513&action=edit
    I think the ID must be 1513 (?)

    It still doesn´t work.

    #1116428
    Tom
    Lead Developer
    Lead Developer

    That’s right – did you also update the page parent ID?

    #1116600
    Regina

    yes both.

    I used the plugin code snippets now and it works. Maybe it was a misunderstanding using two hooks for that?

    #1116863
    Tom
    Lead Developer
    Lead Developer

    Ah yes – filters need to be added in your functions.php file or in Code Snippets 🙂

    #1462109
    jasond

    it is not working with CPT children, can you advise how to make it work?

    Thanks a lot

    #1462169
    David
    Staff
    Customer Support

    Hi there,

    this line:

    if ( 1180 === $element_id && ( is_page() && $post->post_parent == '415' ) ) {

    one of the conditions is_page() needs to be changed for is_singluar('your-cpt'):

    https://developer.wordpress.org/reference/functions/is_singular/

    #2350503
    Gabriel

    +1 for pages rules based on parent page

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