Site logo

[Support request] Display rules for elements : regex possible?

Home Forums Support [Support request] Display rules for elements : regex possible?

Home Forums Support Display rules for elements : regex possible?

Viewing 15 posts - 16 through 30 (of 32 total)
  • Author
    Posts
  • #2230935
    David
    Staff
    Customer Support
    #2231391
    Maxime

    Perfect, it works now. The problem was related to the outdated plugin.

    Thank you for your help 🙂

    #2231862
    David
    Staff
    Customer Support

    Awesome – glad we could be of help

    #2234764
    Maxime

    Hello (again),

    My Issue is not totaly solved because in some cases, a child page can have children of their own.

    Example :
    https://mywebsite.com/directory1/page-1/page-11
    https://mywebsite.com/directory1/page-1/page-12
    https://mywebsite.com/directory1/page-1/page-13

    >> I want to apply my rules to all my children and grandchildren pages.

    I found this solution (https://generatepress.com/forums/topic/block-hook-on-all-sub-pages/)with the fonction get_post_ancestors

    in my case the full code is :

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        global $post;
    	$ancestors = get_post_ancestors( 3854 );
    
    if ( 3850 === $element_id && 3854 == $post->post_parent || is_single( $post->post_parent ) || in_array( $post->ID, $ancestors ) ){
    
            $display = true;
        }
        return $display;
    }, 10, 2 );

    As a reminder:
    3850 is my Element ID
    3854 is my parent page ID (/directory1/)

    With this function it still work for the children pages but not for my grandchildren pages.

    >> where is the issue?

    Thank you a lot for your help.

    #2235406
    David
    Staff
    Customer Support

    Give this a shot:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        global $post;
    
        // Get the top level parent ID 
        if ($post->post_parent)  {
            $ancestors=get_post_ancestors($post->ID);
            $root=count($ancestors)-1;
            $parent = $ancestors[$root];
        } else {
            $parent = $post->ID;
        }
    
        if ( is_page() && 5281 === $element_id && '3854' == $parent ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );
    #2235431
    Maxime

    Unfortunately it doesn’t work . (I adapted the ID and removed an extra paranthesis):

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        global $post;
    
        // Get the top level parent ID 
        if ($post->post_parent)  {
            $ancestors=get_post_ancestors($post->ID);
            $root=count($ancestors)-1;
            $parent = $ancestors[$root];
        } else {
            $parent = $post->ID;
        }
    
        if ( is_page() && 3850 === $element_id && '3854' == $parent ) {
            $display = true;
        }
        return $display;
    }, 10, 2 );
    #2235446
    David
    Staff
    Customer Support

    Oops sorry about the extra ).

    I tested this on my local install, with an Element set to Display on the parent and it worked.
    We are dealing with static pages right ?

    #2235467
    Maxime

    Yes we are dealing with static pages.

    #2235494
    David
    Staff
    Customer Support

    before this line:

    if ( is_page() && 3850 === $element_id && '3854' == $parent ) {

    can you add:

    var_dump($parent);

    Does this return any data when you view the parent or child pages on the front end ?

    #2235503
    Maxime

    It doesn’t work but Curently the following characters are displayed at the top of the pages in front :
    int(9) int(9) int(9) int(9) int(9) int(9) int(9) int(9) int(9)

    These characters are the same for all pages in the directory (children pages and grandchildren pages). But logically different (I think) for other pages.

    #2235596
    David
    Staff
    Customer Support

    That INT – should be the Page ID of the Parent page – can you try that in the condition ?

    #2235635
    Maxime

    Yes it is the ID of my level 1 parent. I forgot to specify one level in my url. The complete URL is : https://mywebsite.com/section-1/directory1/page-1/page-11

    “Section1” ID : 9
    “Directory1” ID : 3854

    And now, I have the result I wanted, Thank you 🙂

    #2236319
    David
    Staff
    Customer Support

    Glad to be of help!

    #2392199
    Maxime

    Hello,

    I am re-launching an old topic because I would like another clarification:

    This rule is working well :

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

    But is it possible to exclude a specific page from this rule ? (in order to assign another element in the back office). I tried to modify my condition but nothing change (I want to exclude the page which the ID is 35):

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        global $post;
    
        if ( 490 === $element_id && ( is_page() && $post->post_parent == '9' ) && ( is_page() && $post->post !== '35' )) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );

    What is the issue?

    Thank you for your help.

    #2392689
    David
    Staff
    Customer Support

    Hi there,

    try:

    
    add_filter( 'generate_element_display', function( $display, $element_id ) {
        global $post;
    
        if ( 490 === $element_id && ( is_page() && $post->post_parent == '9' ) && !is_page( 35 ) ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );
Viewing 15 posts - 16 through 30 (of 32 total)
  • You must be logged in to reply to this topic.