Skip to content

Instantly share code, notes, and snippets.

@patric-boehner
Last active December 15, 2018 05:55
Show Gist options
  • Save patric-boehner/c3540e4b601a760d46759196f77bb1a1 to your computer and use it in GitHub Desktop.
Save patric-boehner/c3540e4b601a760d46759196f77bb1a1 to your computer and use it in GitHub Desktop.
Add a list of child pages to the Genesis theme sidebar
<? php
// Don't include opening php tag
// Add subpages to sidebar
function pb_add_sub_pages_navigation() {
// Don't do anything if not a page
if ( ! is_page() ) {
return;
}
global $post; // Setup the global variable $post
/*
* The title addition is optional. For the theme I was creating we wanted the child page navigation
* to have a consistent heading, for example "Paitent Information". Some parent pages had this
* Syntax allready and some did not. So we filter the parent page title and add on our addition
* if it's not present.
*/
// Get the parent page title
$parent_title = get_the_title($post->post_parent);
$title_addition = __( 'Information', 'daniel-mintie' );
// Search and append parent page title for use in sidebar title
if ( stripos( $parent_title, 'information' ) !== false ) {
$parent_title = $parent_title;
} else {
$parent_title = $parent_title. ' ' .esc_html($title_addition);
}
// Make sure we are on a page and that the page is a parent.
if ( is_page() && $post->post_parent ) {
$sub_pages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
} else {
$sub_pages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
}
if ( $sub_pages ) {
// Include subpages view
include CHILD_DIR . '/inc/views/subpages-menu.php';
}
}
// Hook our sidebar logic to get_header
add_action( 'get_header', 'pb_swap_primary_sidebar' );
function pb_swap_primary_sidebar() {
// Only want this to run on pages.
if ( is_page() ) {
//Remove primary sidebar
remove_action( 'genesis_sidebar', 'genesis_do_sidebar' );
// Add childpage sidebar
add_action( 'genesis_sidebar', 'pb_add_sub_pages_navigation' );
}
}
<div class="subpages-widget-area widget-area">
<section class="widget subpages">
<div class="widget-wrap">
<h3 class="widgettitle widget-title">
<?php echo $parent_title; ?>
</h3>
<ul class="sub-menu">
<?php echo $sub_pages; ?>
</ul>
</div>
</section>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment