Archived topic
using hooks on a template page
5 replies · Started by Davide on April 16, 2021
Hi, is there a way to use an hook (in this case generate_menu_bar_items) only on a custom template?
Under the "display rules" in the Element menu there isn't this possibility, so I guess it should be added via code (using something like https://developer.wordpress.org/reference/functions/is_page_template/ ? I'm not sure).
Any hint?
thank you!
David
Hi David,
Not 100% sure what you are after but would this filter be helpful?
https://docs.generatepress.com/article/generate_hook_element_display/
It can be used when the display rules don't cover the condition you are after.
Let me know :)
Hi Leo, thank you.
not sure about that https://docs.generatepress.com/article/generate_hook_element_display/ but I'll check it out on Monday.
I'm trying to display a "generate_menu_bar_items" hook on pages which are using the template (let's say) layout-example.php.
Maybe this could be achieved directly in the template (layout-example.php) itself..
thanks.
Have a nice weekend
David
Perhaps something like this?
add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
if ( 10 === $element_id && is_page_template( 'layout-example.php' ) ) {
$display = true;
}
return $display;
}, 10, 2 );
Hi Leo,
perfect, thanks!
David
No problem :)