[Resolved] Can I use an image from an Advance Custom Field for my header background?

Home Forums Support [Resolved] Can I use an image from an Advance Custom Field for my header background?

Home Forums Support Can I use an image from an Advance Custom Field for my header background?

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #868033
    Melissa

    I need to use a different for the featured image used in blog grids and the hero image on the blog post page. I can create a “hero image” ACF, but how could I use that as an option in my header Element background?

    Thanks!

    #868185
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    You can do something like this:

    add_filter( 'generate_page_hero_background_image_url', function( $url ) {
        $background_image = get_post_meta( get_the_ID(), 'your_custom_field', true );
    
        if ( $background_image ) {
            $url = $background_image;
        }
    
        return $url;
    } );

    Just updated your_custom_field with the name of your custom field.

    Let me know 🙂

    #868683
    Melissa

    Should that give me the option in the drop-down menu on the edit header element page?

    I added your filter with “Code Snippets” and can’t see any obvious effect. Was that the wrong place to add it?

    #868846
    Tom
    Lead Developer
    Lead Developer

    No, that should automatically use a different background image URL if it’s set as a custom field.

    For example, if you have a Header applied to the “About” page, and the About page has a custom field with a URL in it, it would use that URL.

    Let me know if that’s not what you were after 🙂

    #868908
    Melissa

    Ok, I’ve got it working nicely now. The problem seems to be that I was using the custom field type “image” not URL.

    Thanks so much!

    #868988
    Tom
    Lead Developer
    Lead Developer

    You’re welcome 🙂

    #1072146
    Rafał

    Neat! Works for Elements’ Header.
    Tom, can you give us a solution for .inside-article (or any other container addressed by a class or id), please?

    #1072448
    David
    Staff
    Customer Support

    Hi there,

    something like this hooked into the WP_head :

    <?php
    $background_image = get_post_meta( get_the_ID(), 'your_custom_field', true );
    
    if ( $background_image && is_single() ) {
        echo '<style>
        .inside-article {
            background-image: url("' . $background_image . '");
        }';
    }
    
    ?>
    #1073459
    Rafał

    Thank you, David!!
    I’ve used Elements’ Hook, chosen Display Rules, and to push it to the wp_head needed to cut off the check if is_single().
    Closing tag </style> was missed.
    This works for me:

    <?php
    $background_image = get_post_meta( get_the_ID(), 'my_custom_field', true );
    
    if ( $background_image ) {
    echo '
    <style id="my-id" type="text/css">
    	.inside-article {
    		background-image: url("' . $background_image . '");
    	}
    </style>
    ';
    }
    ?>
    
    #1075091
    David
    Staff
    Customer Support

    Ooops – thanks for updating an glad you got it resolved.

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