-
-
Save braddalton/5916043 to your computer and use it in GitHub Desktop.
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 include php tag | |
// register sidebar for the middle of the posts in home or archive page | |
genesis_register_sidebar( array( | |
'id' => 'sidebar-somewhere-in-the-posts', | |
'name' => __( 'Middle of Posts Widget Area' ), | |
'description' => __( 'Display widgets in the middle of your posts on the home or archive pages.' ), | |
) ); | |
add_action( 'genesis_before_post', 'display_somewhere_in_posts_sidebar' ); | |
add_action( 'genesis_before_entry', 'display_somewhere_in_posts_sidebar' ); | |
/** | |
* Callback for Genesis Framework 'genesis_before_post' or 'genesis_before_entry' action. | |
* | |
* Display widget area on home page or archive pages within the middle of the posts. | |
* | |
* @author Ryan Meier http://www.rfmeier.net/ | |
* | |
* @global object $wp_query the WP_Query object | |
* @return null exit | |
*/ | |
function display_somewhere_in_posts_sidebar(){ | |
// if home page or archive page... | |
if( is_archive() ){ | |
global $wp_query; | |
global $posts; | |
// check for valid current_post (index) | |
if( ! isset( $wp_query->current_post ) ) | |
return; | |
// get posts per page | |
$posts_per_page = (int) get_option( 'posts_per_page' ); | |
// if empty posts per page, return | |
if( empty( $posts_per_page ) ) | |
return; | |
// if not a full page of posts, return | |
if( count( $posts ) < $posts_per_page ) | |
return; | |
// determine the middle-ish index | |
$middle_index = intval( ceil( $posts_per_page / 2 ) ); | |
// if invalid middle index, return | |
if( empty( $middle_index ) ) | |
return; | |
// if the middle-ish index post ID is NOT the current post ID, return | |
if( $middle_index != $wp_query->current_post ) | |
return; | |
// display the widget area | |
genesis_widget_area( 'sidebar-somewhere-in-the-posts' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment