Archived topic
Display rules for elements : regex possible?
31 replies · Started by Maxime on May 3, 2022
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
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 :)
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
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;
}
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
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
Hi Ying,
Yes I tried to setting empty or pick other elements (all websites, pages...). but nothing change.
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 ?
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?
Ah I see, header element doesn't allow publish without location.
Can you try using block element?
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.
Send us a temporary admin login and we can take a look.
You can share the login details in the Private Information field.
Thank you, I shared the access
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 ?
Oh yes,I updated the plugin with the version 2.1.2