Site logo

Archived topic

Display rules for elements : regex possible?

31 replies · Started by Maxime on May 3, 2022

Viewing posts 16–30 of 32

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

Thank you for your help :)

Awesome - glad we could be of help

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.

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 );

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 );

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 ?

Yes we are dealing with static pages.

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 ?

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.

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

Glad to be of help!

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.

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 );
This archived topic is closed to new replies.