[Resolved] Block-Hook on All Sub-Pages

Home Forums Support [Resolved] Block-Hook on All Sub-Pages

Home Forums Support Block-Hook on All Sub-Pages

Viewing 15 posts - 1 through 15 (of 21 total)
  • Author
    Posts
  • #2086111
    Math

    Hi,

    Using Elements, would it be possible to add a headline block on all pages under a parent page?

    I would like to have 10 different pages under a parent page to have the exact same headline.

    Thank you.

    #2086138
    David
    Staff
    Customer Support

    Hi there,

    you would create a Block Element that has no Display Rule locations – just leave them blank.
    And take a note of the Element ID ( in the browser URL field ), then you need to get the ID of the Parent page.

    And lastly a PHP Snippet to enable the element on children of that arent:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        global $post;
        if ( 100 === $element_id && 123 == $post->post_parent ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );

    In this line: 100 === $element_id && 123 == $post->post_parent you need to update the 100 to match your element ID and the 123 for your parent ID

    #2086382
    Math

    Hi David,

    I got the ID and I want to create another element to execute the PHP.

    <?php 
    	add_filter( 'generate_element_display', function( $display, $element_id ) {
    			global $post;
    			if ( 11232 === $element_id && 11128 == $post->post_parent ) {
    					$display = true;
    			}
    
    			return $display;
    	}, 10, 2 ); ?>

    I added the code above to the new element and put the display rule for the entire site and it’s still not working. Did I do something wrong?

    #2086529
    David
    Staff
    Customer Support

    That PHP snippet cannot be added to an Element.
    This doc explains how to add it to the site:

    https://docs.generatepress.com/article/adding-php/

    #2086533
    Math

    I put it in the functions.php in the Child Theme and doeesn’t appear to be working. Here’s the page.

    #2086704
    Elvin
    Staff
    Customer Support

    Hi Math,

    Can you verify what’s the ID of the GP element to be displayed? and the ID of the post parent?

    We can help you verify it if you can provide us temporary backend access. (you can provide it through the private information text field)

    Let us know. 😀

    #2086711
    Math

    GP element – 11232 (Name-GenerateBlocks Customization Tips)
    Parent Post – 11128 (Name-GeneratePress Customization Tips)

    Login Info provided.

    #2086744
    Elvin
    Staff
    Customer Support

    I see,

    11232 is indeed the GP element so this part – 11232 === $element_id – is correct.

    11128 is the ID of — GeneratePress Customization Tips which is the parent of the page you’ve linked so the 11128 == $post->post_parent part is correct as well.

    I’ve checked the page itself and I see the GP element 11232 as shown here – https://share.getcloudapp.com/GGuEQ6KQ

    I’m not sure I see what the issue is. Perhaps you’ve already sorted this out?

    #2087077
    Math

    Hi Elvin,

    The issue isn’t resolved. The header block I created in the elements doesn’t display on parent page or any child pages.

    I should be able to see it on this page but it doesn’t display anything.

    The reason you saw it previously was because I was testing it out by manually inserting the header block on the page.

    Thank you.

    #2087103
    David
    Staff
    Customer Support

    Is that page a custom post type ? If so – how is the child parent relationship being set ?

    #2087165
    Math

    Yes, it’s a custom post type. I used Custom Post Type UI to create it and I set the Capability type to “page” and enabled “Hierarchical”.

    You can login and take a look.

    #2087246
    David
    Staff
    Customer Support

    That should work… however…

    I checked the ID of the parent page you provided here and its 11135

    Can you double check the IDS and update the snippet

    #2087759
    Math

    Hi David,

    I changed it 11135 and still doesn’t work.

    Just for clarification, I’m looking to apply to the following hierarchy.

    Main Page (Parent)
    Sub Page (Child page – I want everything under this page to have the same element header, not just this page)
    Sub Sub Page 1
    Sub Sub Page 2
    Sub Sub Page 3

    Is this possible?

    #2088103
    Elvin
    Staff
    Customer Support

    To clarify further:

    You want the element to display on the child pages AND its parent pages?

    As it is, the condition is only set for child pages. If you want to include the parent pages, you must add it in the condition.

    You can either add is_page() if it’s a static page or is_single($post->post_parent).

    Example:

    if ( 11232 === $element_id && 11128 == $post->post_parent || is_single( $post->post_parent ) )

    Or if you want ALL the ancestor posts then you can use get_post_ancestors().

    You set the variable.

    $ancestors = get_post_ancestors( 11232 );

    And then you check if the current post is in the $ancestor array of posts w/ in_array( $post->ID, $ancestors )

    So the condition would look like:

    if ( 11232 === $element_id && 11128 == $post->post_parent || is_single( $post->post_parent ) || in_array( $post->ID, $ancestors ) )

    Which means “if the GP element id is 11232 and 11128 is the post parent OR if the current post is the post parent OR if the post is in this array of posts which is the ancestor of 11232.

    #2088871
    Math

    Hi Elvin,

    Thank you for the detailed explanation. Few clarification points and issues.

    I’m using a custom post type and it’s taking the properties of a typical Page (static). I’m looking to add a header to all the ancestor pages.

    Main Page (Parent)
    Sub Page (Child page – I do not want the header on this)
    Sub Sub Page 1 (Want header here)
    Sub Sub Page 2 (Want header here)
    Sub Sub Page 3 (Want header here)

    You explained above that you set the variable to the following:
    $ancestors = get_post_ancestors( 11232 );
    But this is the element ID, not the Page ID.

    Also, I tried without the code to see if I can apply an Element header directly to a page by indicating a Location, and it’s not being applied to any of the pages within my custom post type.

    Can Elements not be used with custom post types?

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