Last active
May 21, 2019 04:56
-
-
Save sirchrispy/201136bf063c0e6b8a56a0828128ad10 to your computer and use it in GitHub Desktop.
Creates a widget area and displays it after every X number of posts in a Genesis theme.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//* Do not copy the beginning <?php tag; place this code in your functions.php file | |
// Step 1: Modify the loop to accept an action hook | |
// Step 2: Create a widget area | |
// Step 3: Have the loop recognize the widget area | |
// Step 4: Place something in the new widget area to display | |
// Step 5: Style the area | |
/** | |
* Step 1: Modify the loop to accept an action hook | |
* | |
* Display an ad after every 4th post on an archive page | |
* | |
* @author Chris Mower | |
* @package [Genesis Child Theme] | |
*/ | |
add_action( 'genesis_after_entry', 'cm_ads_in_the_loop' ); | |
function cm_ads_in_the_loop() { | |
// make $loop_counter available to this function | |
global $loop_counter; | |
// start incrementing the counter | |
$loop_counter++; | |
// once the loop counter gets to four... | |
if( $loop_counter == 4 ) { | |
// and if it's on one of these page types... | |
if( is_archive() || is_home() || is_search() ) { | |
// then display whatever we want to hook into here. | |
do_action( 'cm_in_the_loop' ); // yay, we have a new hook! | |
} | |
// set the counter back to zero | |
$loop_counter = 0; | |
} | |
} | |
/** | |
* Step 2: Create a widget area | |
* | |
* This widget area will be used to display ads, but you can rename and | |
* use it for whatever your heart so desires! | |
* | |
* @author Chris Mower | |
* @package [Genesis Child Theme] | |
*/ | |
genesis_register_sidebar( array( | |
'id' => 'loop-ads', | |
'name' => __( 'Loop Ads', 'cm-genesis' ), | |
'description' => __( 'Use a \'Custom HTML\' widget and place your ad code within it. Recommended ad size: 728 x 90 Leaderboard.' ), | |
) ); | |
/** | |
* Step 3: Have the loop recognize a widget area | |
* | |
* Now that a sidebar is created, we're going to place it into | |
* the loop using our newly created hook. We're using Genesis | |
* markup so we have uniformity betwixt widget areas. | |
* | |
* @author Chris Mower | |
* @package [Genesis Child Theme] | |
*/ | |
add_action( 'cm_in_the_loop', 'cm_place_widget_in_archives'); | |
function cm_place_widget_in_archives() { | |
// only display this if something is in the loop-ads sidebar | |
if ( is_active_sidebar( 'loop-ads' ) ) { | |
// Genesis markup for creating a widget area and tying it to | |
// the sidebar we want | |
genesis_widget_area( 'loop-ads', array( | |
'before' => '<div class="loop-ads-widget-area"><div class="loop-ads widget-area">', | |
'after' => '</div></div>', | |
) ); | |
} | |
} | |
/** | |
* Step 4: Place something in the new widget area to display | |
* | |
* No function here. Go to Appearance > Widgets and add something to | |
* your new sidebar, then go check out your archive pages. | |
* | |
* @author Chris Mower | |
* @package [Genesis Child Theme] | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Don't forget to style your new widget area! | |
* For starters, we'll just make sure it's centered. | |
*/ | |
.loop-ads-widget-area { | |
margin: 0 auto 40px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment