Skip to content

Instantly share code, notes, and snippets.

@robneu
Created July 22, 2014 23:32
Show Gist options
  • Select an option

  • Save robneu/c9c8af7c5e6094a285fd to your computer and use it in GitHub Desktop.

Select an option

Save robneu/c9c8af7c5e6094a285fd to your computer and use it in GitHub Desktop.
Sometimes you might want to conditionally load something for the "blog" section of your Genesis website. As sites get more complex, it can be useful to load different elements conditionally and avoid duplicating code. This is one example of how you can do that.
<?php
/**
* Helper function to determine if we're on a blog section of a Genesis site.
*
* @return bool True if we're on any section of the blog.
* @author Robert Neu
*/
function prefix_is_blog() {
$is_blog = array(
'blog_template' => is_page_template( 'blog.php' ),
'single_post' => is_singular( 'post' ),
'archive' => is_archive() && ! is_post_type_archive(),
'home' => is_home() && ! is_front_page(),
);
if ( in_array( true, $is_blog ) ) {
return true;
}
return false;
}
// Usage Example:
add_action( 'genesis_after_header', 'prefix_change_blog_sidebar' );
//* Swap default sidebar for the blog sidebar on blog posts and archives
function prefix_change_blog_sidebar() {
if ( ! prefix_is_blog() ) {
return;
}
if ( is_active_sidebar( 'blog-sidebar' ) ) {
remove_action( 'genesis_sidebar', 'genesis_do_sidebar' );
add_action( 'genesis_sidebar', 'prefix_do_blog_sidebar' );
}
}
//* Display a blog sidebar.
function prefix_do_blog_sidebar() {
dynamic_sidebar( 'blog-sidebar' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment