Site logo

Archived topic

generate_hook_element_display

10 replies · Started by Dan on December 31, 2020

Viewing posts 1–11 of 11

Hi,
I would like to display a block in a hook location but only if an ACF field is 'True'
So this is the current code in functions.php:

add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
    if ( 7314 === $element_id && get_field( 'show_product_carousel' ) ) {
        $display = true;
    }

    return $display;
}, 10, 2 );

The ACF true/false field is called 'show_product_carousel'
The element ID is 7314

In the element itself i did not set a location, the hook is set to generate_before_footer

If I set the location in the element setting I do see the block.
Any ideas why the function is not working for me?

Thanks and Happy new year!

Dan

Hi there,

From the looks of it, it should work.

You can debug it inside the function like this:

var_dump( $element_id );

Does that output the correct ID?

And this:

var_dump(get_field( 'show_product_carousel' ));

Does it output the desired result?

Happy New Year!

Thanks Tom,
Nothing comes up with the debugging codes:

 
add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
    if ( 7314 === $element_id && get_field( 'show_product_carousel' ) ) {
        var_dump( $element_id );
		var_dump(get_field( 'show_product_carousel' ));
		
		$display = true;
    }

    return $display;
}, 10, 2 );

Hi there,

does the post you're viewing have a True / False value set in the ACF field ? As it may be returning Null if that is unset.

Try adding them outside the conditional:

add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
    var_dump( $element_id );
    var_dump(get_field( 'show_product_carousel' ));

    if ( 7314 === $element_id && get_field( 'show_product_carousel' ) ) {
        $display = true;
    }

    return $display;
}, 10, 2 );

I'm seeing:
int(7145) bool(true)

so 7145 is the wrong element ID. It should be 7314
I wonder why it's taking the wrong number.

add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
	var_dump( $element_id );
	var_dump(get_field( 'show_product_carousel' ));
    if ( 7314 === $element_id && get_field( 'show_product_carousel' ) ) {
      
		
		$display = true;
		
    }

    return $display;
}, 10, 2 );

Element ID:
https://www.dropbox.com/s/ku8w1c1d5swcutq/element-id.jpg?dl=0

7145 is the 'Below header BG' element

Aha, that's the Hook Element filter, but this is a Block Element.

Try this filter instead: generate_block_element_display

A-Ha!
It's working now (of course), returning:
int(7179) bool(true) int(7314) bool(true)

Just a recap what I'm using it for - if others are interested.
On a website with products, we wanted to show a carousel with products at the end of pages, but to give the site's admin an option to show them or not.
So I created a block element with a carousel of products.
I also created a ACF field - true/false which will essentially tie in the block element to display or not via the generate_block_element_display filter. Similar to the hook filter, but with a block:
https://docs.generatepress.com/article/generate_hook_element_display/
Now, the site's admin has an option to show or not to show the product carousel at the end of the page, currently set to before footer (or wherever we set the hook to be).
See more screenshot in this thread above.

Thanks again for all you help and support (even on a New Years weekend), not taken for granted.

Have a fabulous 2021!

Dan

Awesome, glad it worked. Thanks for sharing more details with us!

Thanks! You too :)

This archived topic is closed to new replies.