Many Genesis child themes (e.g., Altitude Pro, the current theme for this Web site) natively implement what I call dynamic linear gradients over background images in homepage sections. The effect is that as the visitor scrolls down the home page, the background image gradually changes appearance, typically getting increasingly darker (see below).
But not all Genesis child themes natively support this effect. One such theme, which I’m using for a client project, is Centric Pro.
A Look Under the Hood
In order to make the Centric Pro homepage backgrounds act the ones in Altitude Pro, I inspected what’s under the hood in Altitude Pro.
Both themes support multiple stackable home-page sections, each of which is contained inside a div
.
- Altitude natively supports up to 7 home-page sections, named “Front Page 1”, “Front Page 2”, and so on to “Front Page 7”. The divs have corresponding class names: “front-page-1”, “front-page-2”, and so on to “front-page-7”.
- Centric natively supports up to 6 home-page sections, named “Home 1”, “Home 2”, and so on to “Home 6”. Its divs also have corresponding class names: “home-widgets-1”, “home-widgets-2”, and so on to “home-widgets-6”.
What’s distinguishes Altitude from Centric, and what allows the dynamic gradient to work in Altitude is a div
container inside each “front-page-n” div:
<div id="front-page-1" class="front-page-1" tabindex="-1"> <div class="image-section"> <!-- Contents of widget goes here --> </div> </div>
The background image for each section that has one is attached to its respective “front-page-n” div, like so:
.front-page-1 { background-image: url(../../uploads/standout-1650x1050-low.jpg); }
The linear gradient is attached to the inner div with the class of “image-section”:
.image-section { background: linear-gradient(to bottom, rgba(0,0,0,0) 0%,rgba(0,0,0,0.2) 30%,rgba(0,0,0,0.6) 80%,rgba(0,0,0,0.8) 100%); }
Customizing Centric to Support the Dynamic Linear Gradient Effect
For this project, the only home-page section I want to add the dynamic linear background effect is “home-widgets-5”.
Clearly, all I have to do to make Centric behave like Altitude is alter the code that generates the “home-widgets-n” elements.
That native code is found in the front-page.php
file in the Centric folder:
function centric_home_widgets() { echo '<div id="home-widgets" class="home-widgets">'; genesis_widget_area( 'home-widgets-2', array( 'before' => '<div class="home-widgets-2 widget-area">', 'after' => '</div>', ) ); genesis_widget_area( 'home-widgets-3', array( 'before' => '<div class="home-widgets-3 color-section widget-area">', 'after' => '</div>', ) ); genesis_widget_area( 'home-widgets-4', array( 'before' => '<div class="home-widgets-4 dark-section widget-area">', 'after' => '</div>', ) ); genesis_widget_area( 'home-widgets-5', array( 'before' => '<div class="home-widgets-5 widget-area">', 'after' => '</div>', ) ); genesis_widget_area( 'home-widgets-6', array( 'before' => '<div class="home-widgets-6 color-section widget-area">', 'after' => '</div>', ) ); echo '</div>'; }
The before
and after
elements of the generate_widget_area
function define the wrapper for the section. All I have to do is add a bit of html, like so:
genesis_widget_area( 'home-widgets-5', array( 'before' => '<div class="home-widgets-5 widget-area"><div class="image-section">', 'after' => '</div></div>', ) ); }
Now I can replicate the CSS used in Altitude to achieve the desired effect.
Moving the Customization to a Function
I generally don’t like to modify Genesis child theme files — even though they are technically part of a child theme, and child themes is where we’re supposed to do our customizations. However, in the event the child theme is ever updated (and they are, from time to time), I don’t want to lose the changes.
So I created a separate file (nwb-functions.php
) in the Centric folder where I wrote a couple of functions to achieve the customization. (Yes, I did have to add a line to the functions.php
file in the Centric folder to include my new file.)
Here’s the code in my new functions file:
function nwb_centric_home_widgets() { echo '<div id="home-widgets" class="home-widgets">'; genesis_widget_area( 'home-widgets-2', array( 'before' => '<div class="home-widgets-2 widget-area">', 'after' => '</div>', ) ); genesis_widget_area( 'home-widgets-3', array( 'before' => '<div class="home-widgets-3 color-section widget-area">', 'after' => '</div>', ) ); genesis_widget_area( 'home-widgets-4', array( 'before' => '<div class="home-widgets-4 dark-section widget-area">', 'after' => '</div>', ) ); genesis_widget_area( 'home-widgets-5', array( 'before' => '<div class="home-widgets-5 widget-area"><div class="image-section">', 'after' => '</div></div>', ) ); genesis_widget_area( 'home-widgets-6', array( 'before' => '<div class="home-widgets-6 color-section widget-area">', 'after' => '</div>', ) ); echo '</div>'; } add_action('wp_head', 'nwb_home_widgets'); function nwb_home_widgets() { remove_action( 'genesis_before_footer', 'centric_home_widgets', 5 ); add_action( 'genesis_before_footer', 'nwb_centric_home_widgets', 5 ); }
Lines 1-24, nwb_centric_home_widgets
, are a clone of the code from front-page.php
, with lines 16 and 17 modified as shown earlier. Then I created a container function, nwb_home_widgets
(lines 26-29) which unhooks the centric-home-widgets
function from the genesis_before_footer
hook (line 27) and hooks the nwb_centric_home_widgets
function to that hook (line 28). The code on line 25 invokes nwb_home_widgets
.
Join the Discussion
Let me know what you think. If you have questions, I’ll be happy to address them. If you have another way to achieve this dynamic linear gradient effect, I’d love to hear it.
Leave a Reply