Archived topic
Apply Same Element for WPML Translated Pages
5 replies · Started by Leon on July 17, 2022
Hi Team,
I've recently translated my website using WPML and I'm having some issues with Elements.
E.g. For "Element 1", I've set to apply a hook globally and excluded Page A. After Translation, the "Page A Translation Page" was not excluded from "Element 1", which causes lots of repeat work since I have over 120 pages to manually exclude them.
Is there a way to make it that "Elements" are applied to certain pages and their Translations?
Thank you
Hi Leon,
Yes, there’s a way,
There are these filters:
generate_block_element_display
generate_hook_element_display
generate_header_element_display
generate_layout_element_display.
Reference on how to use it: https://docs.generatepress.com/article/generate_element_display/
So for instance, you have a Block Element you want to display for post with ID of 23, the code would be:
add_filter( 'generate_block_element_display', function( $display, $element_id ) {
if ( 100 === $element_id && is_single('23') ) {
$display = true;
}
return $display;
}, 10, 2 );
Adding PHP reference: https://docs.generatepress.com/article/adding-php/#code-snippets
Hope this helps!
Hi Fernando,
I still can't figure it out.
My Element Id is 319. It's a hook element applied globally, excluding some pages.
Now I want that hook element to also automatically exclude the "Translated pages" of the above "excluded pages", could you advise the code that I should use in detail?
Thank you!
Try something like:
add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
if ( ! is_admin() && 319 === $element_id && is_page(array(145,123,456)) ) {
$display = false;
}
return $display;
}, 10, 2 );
This would exclude the hook for pages with id - 145, 123 and 456. Kindly replace and add the IDs of the pages to exclude.
Hope this helps!
It works! Thank you Fernando.
You’re welcome Leon!