Home › Forums › Support › Making one page hero element appear using code and prevent all others appearing
- This topic has 13 replies, 3 voices, and was last updated 3 years, 4 months ago by
David.
-
AuthorPosts
-
November 21, 2022 at 6:11 pm #2426428
William
Hi there,
I have some code which is the following roughly:
add_filter( 'generate_element_display', function( $display, $element_id ) { if ( 2603263 === $element_id && ... ) { $display = true; } return $display; }, 10, 2 );The problem is that the existing page hero still appears, so it does not ‘swap’ – is there a way to fix this? I would like it so that if the conditions are met, the element appears and replaces whatever element is in that same location (if that makes sense)?
Kind regards,
Will
November 21, 2022 at 6:50 pm #2426452Fernando Customer Support
Hi Will,
You’ll need to declare the Element to be replaced in your filter as well.
For instance, the code can look like this:
add_filter( 'generate_element_display', function( $display, $element_id ) { if ( ... ) { if ( 2603263 === $element_id ) { $display = true; } else if ( 123456 === $element_id ) { $display = false; } } return $display; }, 10, 2 );Replace
...with your condition and123456with the other element ID.If it’s multiple IDs, the code would look like this:
add_filter( 'generate_element_display', function( $display, $element_id ) { if ( ... ) { if ( 2603263 === $element_id ) { $display = true; } else if ( in_array( $element_id, array( 123456, 7891011) ) ) { $display = false; } } return $display; }, 10, 2 );November 22, 2022 at 4:41 am #2427003William
That seems to work well thanks! I am using the array code as have two elements to replace:
– 2553606 which the code is working good for replacing
– 2553607 which is not being replaced yet.I do have some extra code for the above elements which is:
Don’t display element if there is not a featured image
add_filter( 'generate_header_element_display', function( $display, $element_id ) { if ( 2553606 === $element_id ) { // Only target specific Element if ( ! has_post_thumbnail() ) { $display = false; } } return $display; }, 10, 2 );Display the element in a slightly different location:
add_filter( 'generate_page_hero_location', function( $display, $element_id ) { if ( 2553607 === $element_id ) { return 'generate_before_content'; } return 'generate_after_header'; }, 10, 2 );Hopefully that makes sense – what reason could cause 2553607 to still appear even when the code states it should not be?
Many thanks,
Will
November 22, 2022 at 7:27 am #2427252David
StaffCustomer SupportHi there,
that first snippet is targeting the
2553606element ID …. is that the issue? Or have i misread this lolNovember 22, 2022 at 8:05 am #2427486William
The element 2553606 is being replaced correctly. However, when this is not a featured image 2553607 is still appearing when it should not. It has this code so I just wonder what is causing the new element to appear, when the conditions are satisfied (2603263), but still the old element appearing (2553607):
add_filter( 'generate_page_hero_location', function( $display, $element_id ) { if ( 2553607 === $element_id ) { return 'generate_before_content'; } return 'generate_after_header'; }, 10, 2 );November 22, 2022 at 8:42 am #2427572David
StaffCustomer SupportTheres no relation between those two elements, if you want
2553607to display in a different location when there is no featured image then you should do:add_filter( 'generate_page_hero_location', function( $display, $element_id ) { if ( 2553607 === $element_id && ! has_post_thumbnail() ) { return 'generate_before_content'; } return 'generate_after_header'; }, 10, 2 );November 22, 2022 at 9:18 am #2427657William
Ah I think I follow – I think having three if statements is probably not helping after reading your comment, so it would be more efficient and robust to have it so that it is an if, else if, else if statement, rather than three separate if statements?
Thank you for the help!
November 23, 2022 at 2:42 am #2428647David
StaffCustomer SupportSo far i know of the following:
1. Remove
2553606if there post has no featured imageadd_filter( 'generate_element_display', function( $display, $element_id ) { if ( 2553606 === $element_id && ! has_post_thumbnail() ) { // Only target specific Element $display = false; } return $display; }, 10, 2 );2. Move
2553607header element to_before_contentif post has no featured image:add_filter( 'generate_page_hero_location', function( $display, $element_id ) { if ( 2553607 === $element_id && ! has_post_thumbnail() ) { return 'generate_before_content'; } return 'generate_after_header'; }, 10, 2 );Those use separate filter hooks so they cannot be merged into one function.
What is the 3rd condition ?
December 1, 2022 at 11:19 am #2445310William
My conditions are as follows:
if no featured image, move to _before_content
if featured image, use X element
if featured image AND other conditions and no featured image, use Y element
if featured image AND other conditions AND featured image, use Z element
Does that make sense?
December 1, 2022 at 11:46 am #2445376David
StaffCustomer SupportOk, so you have X, Y and Z element.
if no featured image, move to _before_content
Which element is that ?
December 5, 2022 at 7:31 am #2450292William
Ah sorry, that would be another element, call that A if you want!
December 5, 2022 at 8:37 am #2450491David
StaffCustomer SupportOk, so is
Aalways shown even if x, y or z are present ?December 5, 2022 at 2:11 pm #2450842William
Yes that’s correct
December 6, 2022 at 3:52 am #2451333David
StaffCustomer SupportOk, and do you know the additional Conditons for Y and Z ?
Ideally it would be good handle all the logic in one function. -
AuthorPosts
- You must be logged in to reply to this topic.