Skip to content

Instantly share code, notes, and snippets.

@chasereeves
Created January 31, 2012 23:33
Show Gist options
  • Save chasereeves/1713813 to your computer and use it in GitHub Desktop.
Save chasereeves/1713813 to your computer and use it in GitHub Desktop.
Create page sub navigation only for pages that contain and/or are children in Thesis theme.
<?php
/*
* Create page sub navigation only for pages that contain and/or are children in Thesis theme.
* ===========================================================================================
*/
// STEP 1: Create test for whether a page is and/or has children
function is_has_child() {
global $post;
if ( is_page() && $post->post_parent ) return true; // return "true" if current page has a parent
$children = get_pages('child_of='.$post->ID); // return "true"if current page has children
if( count( $children ) != 0 ) return true;
else return false; // return "false" for everything else
}
// STEP 2: Add class "is_has_child" to the body tag for pages that are/have children
function custom_page_bodyclass($classes) {
// is_has_child is true for a parent or a child page
if ( function_exists( 'is_has_child' ) && is_has_child() ) { $classes[] = 'is_has_child'; }
return $classes;
}
add_action('thesis_body_classes', 'custom_page_bodyclass');
// STEP 3: Remove Thesis default sidebars
function remove_thesis_default_sidebars() {
unregister_sidebar( 'sidebar-1' );
unregister_sidebar( 'sidebar-2' );
}
add_action( 'after_setup_theme', 'remove_thesis_default_sidebars' );
// STEP 4: Register our own dynamic sidebars
register_sidebar(array(
'name' => 'Main Sidebar',
'id' => 'sidebar_1',
'description' => 'Shows up on all pages by default.',
'before_title' => '<h3>',
'after_title' => '</h3>'
));
register_sidebar(array(
'name' => 'Subnav Sidebar',
'id' => 'sidebar_2',
'description' => 'Shows up on pages that require a sub-page navigation.',
'before_title' => '<h3>',
'after_title' => '</h3>'
));
// STEP 5: Use different sidebars based on whether or not page is/has children
function custom_sidebar() {
if (is_has_child()) {
// create the sidebar sub-page navigation
global $wp_query;
if( empty($wp_query->post->post_parent) ) { $parent = $wp_query->post->ID; }
else { $parent = $wp_query->post->post_parent; }
if(wp_list_pages("title_li=&child_of=$parent&echo=0" )):
echo "<li class=\"widget widget_subnav\">\n";
echo "\t<h3>Subnav</h3>\n";
echo "\t\t<ul>\n";
wp_list_pages("title_li=&child_of=$parent" );
echo "\t\t</ul>\n";
echo "\t</li>\n";
endif;
// place the new Subnav Sidebar we created in STEP 2
dynamic_sidebar( 'sidebar_2' );
}
else {
// place the new Main Sidebar we created in STEP 2
dynamic_sidebar( 'sidebar_1' );
}
}
// Ta-da!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment