Site logo

[Resolved] Making one page hero element appear using code and prevent all others appearing

Home Forums Support [Resolved] Making one page hero element appear using code and prevent all others appearing

Home Forums Support Making one page hero element appear using code and prevent all others appearing

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

    #2426452
    Fernando
    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 and 123456with 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 );
    #2427003
    William

    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

    #2427252
    David
    Staff
    Customer Support

    Hi there,

    that first snippet is targeting the 2553606 element ID …. is that the issue? Or have i misread this lol

    #2427486
    William

    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 );
    #2427572
    David
    Staff
    Customer Support

    Theres no relation between those two elements, if you want 2553607 to 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 );
    #2427657
    William

    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!

    #2428647
    David
    Staff
    Customer Support

    So far i know of the following:

    1. Remove 2553606 if there post has no featured image

    
    add_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 2553607 header element to _before_content if 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 ?

    #2445310
    William

    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?

    #2445376
    David
    Staff
    Customer Support

    Ok, so you have X, Y and Z element.

    if no featured image, move to _before_content

    Which element is that ?

    #2450292
    William

    Ah sorry, that would be another element, call that A if you want!

    #2450491
    David
    Staff
    Customer Support

    Ok, so is A always shown even if x, y or z are present ?

    #2450842
    William

    Yes that’s correct

    #2451333
    David
    Staff
    Customer Support

    Ok, and do you know the additional Conditons for Y and Z ?
    Ideally it would be good handle all the logic in one function.

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