-
michell
I want to use a specific GP element. So I was wondering whether it was possible to trigger a specific elements by a specific URL.
-
Hi there,
See GP Elements :
https://docs.generatepress.com/article/elements-overview/
Here you can hook your own elements in, and set the Display Rules Location to the specific page, post, archive etc.
If its not possible using the Elements Display Rules, then you can use the
generate_element_display
filter – see here:https://docs.generatepress.com/article/generate_element_display/
And my reply here provides an example of how to check for a specific URL.
-
michell
Hi David,
I add the URL and the element id in the Code snippets plugin but it does not work.
The element I am using is:
Archive Nederland [ID 404278]I want to show the element at this url:
https://heerlijkhuisjehuren.nl/vakantiehuizen/?wpv-relationship-filter-landen=332491&wpv_view_count=63894Can you see if I’m doing something wrong?
Hopefully you can help me.
-
Can you share your final code snippet here ?
-
michell
add_filter( 'generate_element_display', function( $display, $element_id ) { $url = $_SERVER['REQUEST_URI']; if ( 404278 === $element_id && strpos($url, 'https://heerlijkhuisjehuren.nl/vakantiehuizen/?wpv-relationship-filter-landen=332491&wpv_view_count=63894') !== false ) { $display = true; } return $display; }, 10, 2 );
-
Try it this way around.
1. set the Element Display Rules Location to that archive page, so it shows on all instances.
2. then change your snippet to:
add_filter( 'generate_element_display', function( $display, $element_id ) { $url = $_SERVER['REQUEST_URI']; if ( 404278 === $element_id && strpos($url, '?wpv-relationship-filter-landen=332491&wpv_view_count=63894') === false ) { $display = false; } return $display; }, 10, 2 );
Here we check the URL for the Query String, and if it does not exist then we set the element display to false.
-
michell
This works but the country is displayed correctly, but all underlying provinces also use this part of the URL.
country url
?wpv-relationship-filter-landen=332491&wpv_view_count=63894provinces url
?wpv-relationship-filter-landen=332491&wpv_view_count=63894&wpv-relationship-filter=331585
?wpv-relationship-filter-landen=332491&wpv_view_count=63894&wpv-relationship-filter=331629Is it possible to show only the Element for URL:
?wpv-relationship-filter-landen=332491&wpv_view_count=63894 -
You could try this:
add_filter( 'generate_element_display', function( $display, $element_id ) { $url = $_SERVER['REQUEST_URI']; if ( 404278 === $element_id && ( strpos($url, '?wpv-relationship-filter-landen=332491&wpv_view_count=63894&wpv-relationship') === true || strpos($url, '?wpv-relationship-filter-landen=332491&wpv_view_count=63894') === false ) ) { $display = false; } return $display; }, 10, 2 );
It adds an
||
OR condition, first checking if the provinces URL is TRUE, before checking the original string is false. -
michell
Unfortunately, the element is still displayed in the underlying provinces with this code. Is there any other option?
-
In theory the above should work, if the request is built on those query vars, and they are not just doing some front end filtering.
Include this:
var_dump($url);
in the PHP like so:add_filter( 'generate_element_display', function( $display, $element_id ) { $url = $_SERVER['REQUEST_URI']; var_dump($url); if ( 404278 === $element_id && ( strpos($url, '?wpv-relationship-filter-landen=332491&wpv_view_count=63894&wpv-relationship') === true || strpos($url, '?wpv-relationship-filter-landen=332491&wpv_view_count=63894') === false ) ) { $display = false; } return $display; }, 10, 2 );
I will return the $url value on the front end so you can see what the provinces url the server made.
Might give some pointers -
michell
Unfortunately it doesn’t work. Could you please check it for me (Private information below)?
My PHP knowledge is limited.Show Element
https://heerlijkhuisjehuren.nl/vakantiehuizen/?wpv-relationship-filter-landen=332491&wpv_view_count=63894Don’t show Element
https://heerlijkhuisjehuren.nl/vakantiehuizen/?wpv-relationship-filter-landen=332491&wpv_view_count=63894&wpv-relationship-filter=332519I have made a full backup.
-
Test the code with just the Provence code:
add_filter( 'generate_element_display', function( $display, $element_id ) { $url = $_SERVER['REQUEST_URI']; if ( 404278 === $element_id && strpos($url, '?wpv-relationship-filter-landen=332491&wpv_view_count=63894&wpv-relationship') === true ) ) { $display = false; } return $display; }, 10, 2 );
Double check that this
?wpv-relationship-filter-landen=332491&wpv_view_count=63894&wpv-relationship
string in the code is correct.
If it is then that function should remove the element on Provence pages.
If it is do not then that URL query var is not being seen by the$_SERVER['REQUEST_URI'];
- You must be logged in to reply to this topic.