[Resolved] Change page hero background color with Advance Custom fields

Home Forums Support Change page hero background color with Advance Custom fields

  • This topic has 5 replies, 2 voices, and was last updated 1 year ago by David.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #999880
    fredrik

    Hello!
    Is it possible to change the the page hero background (Im using Elements) on page by page basis through Advanced Custom fields (or any other way)

    Im using the current version of generatepress.

    #999919
    David
    Staff
    Customer Support

    Hi there,

    simplest way would be to echo out some CSS into the WP_head hook using the ACF field.
    This article explains how to get the field value of the current post, use the Check if value exists code:

    https://www.advancedcustomfields.com/resources/get_field/

    Then you’re echo statement would look something like this:

    echo '<style>.page-hero {background-color:' . $my_custom_field . ' !important;}</style>';

    #1000583
    fredrik

    My php is knowledge very limited, with that code the echo ''; just shows up in the header.

    #1000641
    David
    Staff
    Customer Support

    It should look something like this:

    <?php
    $color = get_field( "my_custom_field" );
    
    if( $color ) {
        echo '<style>.page-hero { background-color:' . $color . ' !important;}</style>';
    }
    ?>

    It needs to go in a Hook Element, select the WP_Head hook and make sure to check Execute PHP.

    #1000696
    fredrik

    thank you it worked nicely!

    #1000698
    David
    Staff
    Customer Support

    Awesome – glad to be of help.

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