[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 - 1 through 15 (of 32 total)
  • Author
    Posts
  • #2208232
    Maxime

    Hi,

    I would like to use the elements to display a different header image depending the directory of my pages.

    Example :

    https://mywebsite.com/directory1/page-1
    https://mywebsite.com/directory1/page-2
    https://mywebsite.com/directory1/page-3

    >> The same element for all the directory /directory1/

    https://mywebsite.com/directory2/page-a
    https://mywebsite.com/directory2/page-b
    https://mywebsite.com/directory2/page-c

    >> The same element for all the directory /directory2/

    Curently in the display rules I chose “page” and pick each page one-by-one (I have hundreds)

    >> Is it possible to use a regex that with my directory name?

    Thank you for your help

    #2208268
    Leo
    Staff
    Customer Support

    Hi there,

    You can use this filter for advanced display rules:
    https://docs.generatepress.com/article/generate_element_display/

    You will need to find the correct conditional tag to use which is not related to the theme:
    https://codex.wordpress.org/Conditional_Tags

    Hope this helps 🙂

    #2214525
    Maxime

    Hi Leo,

    Thank you, this solution seems to answer my need, but but it doesn’t work. Did I misunderstand something?

    At the end of the function.php file in my child theme I added :

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

    5281 : My Element ID
    3854 : My parent page ID (/directory1/ in my previous example)

    Thank you in advance

    #2214884
    David
    Staff
    Customer Support

    Hi there,

    what happens if you flip it on its head, and set the Element Display rules to display on ALL pages, and set a negative condition to make it false:

    if ( 5281 === $element_id && !( is_page() && $post->post_parent == '3854' ) ) {
        $display = false;
    }
    #2215586
    Maxime

    Hi David,

    I tried :
    – I chose “All website” as display rule
    – I set a negative condition :

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        global $post;
    
    if ( 5281 === $element_id && !( is_page() && $post->post_parent == '3854' ) ) {
        $display = false;
    }
    
        return $display;
    }, 10, 2 );

    >> The function don’t working because my header is displayed on all pages
    Screenshot with my function (sorry back-office in french) : https://ibb.co/6bfZx5C

    #2215710
    Ying
    Staff
    Customer Support

    Hi Maxime,

    When using this PHP snippet, can you make sure the location of the element is set to blank?

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

    Check the screenshot below:
    https://www.screencast.com/t/RGtiPYHp

    #2219757
    Maxime

    Hi Ying,

    Yes I tried to setting empty or pick other elements (all websites, pages…). but nothing change.

    #2220021
    David
    Staff
    Customer Support

    I tested Yings latest version, with the Block Elements display rules set to ‘blank’ and it worked.

    Can you double check the ID of the Element and the Post Parent ?

    #2229977
    Maxime

    I double checked The IDs and I tried with another page and another element(with the corresponding IDs), but no change.

    When I Publish/Update my element, I have the followgin message : https://ibb.co/C11Sgbb
    Could this be an indication regarding this issue?

    #2230137
    Ying
    Staff
    Customer Support

    Ah I see, header element doesn’t allow publish without location.

    Can you try using block element?

    #2230711
    Maxime

    I tried with a “block” type Element but it still doesn’t work : https://ibb.co/GsdrBND

    if it’s easier for you, I can give you a temporary access to the website. Let me know.

    #2230759
    David
    Staff
    Customer Support

    Send us a temporary admin login and we can take a look.
    You can share the login details in the Private Information field.

    #2230856
    Maxime

    Thank you, I shared the access

    #2230903
    David
    Staff
    Customer Support

    Ok – first thing i notice is the GP Premium version is only 1.12 – which is really out of data and the generate_element_display filter wasn’t added until version 2.0.

    Is it possible to update the plugin ?

    #2230922
    Maxime

    Oh yes,I updated the plugin with the version 2.1.2

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