[Resolved] Display Rules for Elements based on Custom Fields?

Home Forums Support [Resolved] Display Rules for Elements based on Custom Fields?

Home Forums Support Display Rules for Elements based on Custom Fields?

Viewing 15 posts - 1 through 15 (of 23 total)
  • Author
    Posts
  • #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!

    #884538
    Tom
    Lead Developer
    Lead Developer

    Hi 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.

    #884654
    Eric

    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…

    #884803
    David
    Staff
    Customer Support

    Hi there,

    that snippet would need to be added to your Child Theme functions.php or added using Code Snippets plugin.

    #885075
    Eric

    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”

    #885697
    Tom
    Lead Developer
    Lead Developer

    Ah, 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 ๐Ÿ™‚

    #885764
    Eric

    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?

    #885964
    Tom
    Lead Developer
    Lead Developer

    I 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?

    #886079
    Eric

    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!

    #886762
    Tom
    Lead Developer
    Lead Developer

    Glad I could help! ๐Ÿ™‚

    #1675353
    Randy

    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?

    #1675811
    David
    Staff
    Customer Support

    Better 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

    #1675849
    Randy

    Thanks David! That is a beautiful solution!!

    #1675874
    David
    Staff
    Customer Support

    Glad to be of help.

    #1701933
    Randy

    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!

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