- This topic has 22 replies, 5 voices, and was last updated 1 year ago by
David.
-
AuthorPosts
-
April 30, 2019 at 3:57 am #884093
Eric
Hey Generatepress team I hope you’re dong well!
I had a question about using custom fields with Elements. I currently have an GP Element created that pulls some text from Advanced Custom Fields across my site. Everything is working quite well and there were no conflicts with GP and ACF or anything like that, but I wanted to know if there is a way for me to set the GP Element to NOT display if there is no data available to pull in the ACF? At the moment, GP defaults to display the Element regardless of whether or not there is data so what happens is I have a bunch of empty HTML tags for all the posts that do not have the ACF defined. What rules should I be setting to achieve this so that the GP Element will just not appear if there is no data to pull? Thanks so much for your time and attention and more power to GENERATEPRESS!
April 30, 2019 at 8:48 am #884538Tom
Lead DeveloperLead DeveloperHi there,
You can use the
generate_header_element_display
filter.For example:
add_filter( 'generate_header_element_display', function( $display, $element_id ) { if ( 123 === $element_id ) { $your_field = get_post_meta( get_the_ID(), 'your_custom_field', true ); if ( ! $your_field ) { $display = false; } } return $display; }, 10, 2 );
You just need to update
123
with the ID of your Element.April 30, 2019 at 10:17 am #884654Eric
Excellent… do I just add this filter snippet to the same ACF Element I’ve created? Or do I need to create a new Hook for this on it’s own? Thank you for the help…
April 30, 2019 at 1:35 pm #884803David
StaffCustomer SupportHi there,
that snippet would need to be added to your Child Theme functions.php or added using Code Snippets plugin.
April 30, 2019 at 9:20 pm #885075Eric
Hey guys I have been able to install the Child theme and paste the code but unfortunately it didn’t work out. The element is still displaying with empty fields.
After experimenting a bit I realized that I am using these as HOOKS across the site and not as HEADER SCRIPT? Does that change the filter in any way? I tried to change out “header_element” with “hook_element” but it gave me an error when I tried to save it:
https://pasteboard.co/IcDr1kK.png
To be exact…
1. I have one Element set as a HOOK to appear on the ENTIRE SITE, “after_main_content”
2. I have another Element set as a HOOK to appear on the ENTIRE SITE, “wp_head”
May 1, 2019 at 8:18 am #885697Tom
Lead DeveloperLead DeveloperAh, can you try this?:
add_filter( 'generate_hook_element_display', function( $display, $element_id ) { if ( 123 === $element_id ) { $your_field = get_post_meta( get_the_ID(), 'your_custom_field', true ); if ( ! $your_field ) { $display = false; } } return $display; }, 10, 2 );
Let me know ๐
May 1, 2019 at 9:15 am #885764Eric
OK this is working better, but a bit TOO well hehe… it’s saving properly now but this removes the Element altogether, whether there IS data or not. So, it just removes the element completely. Is there a way to only remove the Element if there is NO data available in the ACF field, but when there is data available, it will then display the Element?
May 1, 2019 at 2:59 pm #885964Tom
Lead DeveloperLead DeveloperI assume you’ve updated this line to check for your custom field?:
$your_field = get_post_meta( get_the_ID(), 'your_custom_field', true );
If so, are the Display Rules set as well so it will display regardless of the filter?
May 1, 2019 at 7:42 pm #886079Eric
It worked! Sorry, I missed that hehe… all I changed out was “123”. I’ve replaced ‘your_custom_field’ with the name of my ACF and it worked like a charm. Thanks Tom! GENERATEPRESS RULES!
May 2, 2019 at 8:05 am #886762Tom
Lead DeveloperLead DeveloperGlad I could help! ๐
February 27, 2021 at 11:53 am #1675353Randy
It would be awesome if you could add an option in GP Premium Elements’ “Location rules” to use a Custom Field to determine which pages an Element will be displayed on.
I’m working on 20+ similar pages and a half dozen elements, so currently I guess I have to select all 20+ pages on EACH of the 6 Elements Location rules…seems like a perfect job for a custom field, unless you know of a better way?
February 28, 2021 at 3:57 am #1675811David
StaffCustomer SupportBetter to register a custom taxonomy specifically for your Element display rules – example here that applies the tax to both pages and posts:
https://generatepress.com/forums/topic/header-layout-control-for-client/#post-1421567
February 28, 2021 at 4:33 am #1675849Randy
Thanks David! That is a beautiful solution!!
February 28, 2021 at 5:07 am #1675874David
StaffCustomer SupportGlad to be of help.
March 19, 2021 at 6:56 am #1701933Randy
I notice when you register a custom taxonomy as shown in your link you provided here https://generatepress.com/forums/topic/header-layout-control-for-client/#post-1421567
…the custom taxonomies now appear in the default wordpress wp-sitemap.xml file under https://www.example.com/wp-sitemap-taxonomies-element-controls-1.xml (this in turn shows as a 404 error in Google Search Console, which is how I noticed it)
How do we prevent these custom taxonomies from appearing in the wp-sitemap please?
Thanks!
-
AuthorPosts
- You must be logged in to reply to this topic.