Skip to content

Instantly share code, notes, and snippets.

@aijodesigns
Last active August 29, 2015 14:09
Show Gist options
  • Save aijodesigns/f767c1a33fd8b4e84fc5 to your computer and use it in GitHub Desktop.
Save aijodesigns/f767c1a33fd8b4e84fc5 to your computer and use it in GitHub Desktop.
(WordPress) Page Layout with Dynamic Sidebar based on Parent Page
<?php
/**
* Template Name: Page Layout
*/
get_header(); ?>
<div id="main" class="row"><div class="container">
<?php
$children = get_pages('child_of='.$post->ID);
$classes = get_body_class();
if(count($children)!=0 || in_array('page-child',$classes) ) {
$contentClass = ' style=""';
} else {
$contentClass = ' style="width:100%;"';
} ?>
<div class="main-content col-md-9 pull-right" <?php echo $contentClass ?>>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
</div>
<?php
if(count($children)!=0 || in_array('page-child',$classes) ) {
echo get_sidebar();
}
?>
</div></div> <!-- #main -->
<?php get_footer(); ?>
<div class="sidebar col-md-3 pull-left widget-area" id="sidebar" role="complementary">
<h3>In this Section</h3>
<?php /* Creates a menu for pages beneath the level of the current page */
if (is_page() and ($notfound != '1')) {
$current_page = $post->ID;
while($current_page) {
$page_query = $wpdb->get_row("SELECT ID, post_title, post_status, post_parent FROM $wpdb->posts WHERE ID = '$current_page'");
$current_page = $page_query->post_parent;
}
$parent_id = $page_query->ID;
$parent_title = $page_query->post_title;
if ($wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = '$parent_id' AND post_status != 'attachment'")) { ?>
<ul>
<!-- <?php ?>
<li><a href="<?php echo get_permalink($parent_id); ?>"><?php echo $parent_title; ?></a></li>
<?php ?> -->
<?php wp_list_pages('sort_column=menu_order&title_li=&child_of='. $parent_id); ?>
</ul>
<?php } } ?>
</div><!-- end sidebar -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment