Basic PHP script/syntax help in modifying sidebar
-
My objective is ultimately this:
If this is a page then show sub-nav, if not show the normal widgets.Here’s my script so far, I realise I need to add an else in there somewhere but my attempts have failed probably due to my lack of coding experience:
<?php // This creates a sub navigation containing the child pages. if ( is_page() ) { ?> <?php if($post->post_parent) $children = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0'); else $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0'); if ($children) { ?> <div class="childnav"> <h2> <?php $parent_title = get_the_title($post->post_parent); echo $parent_title; ?> </h2> <ul> <?php echo $children; ?> </ul> </div> <?php } } ?> /*an else needs to replace the line above but doing so breaks the code*/ <ul> <?php /* When we call the dynamic_sidebar() function, it'll spit out * the widgets for that widget area. If it instead returns false, * then the sidebar simply doesn't exist, so we'll hard-code in * some default sidebar stuff just in case. */ if ( ! dynamic_sidebar( 'primary-widget-area' ) ) : ?> <li> <?php get_search_form(); ?> </li> <li> <h3><?php _e( 'Archives', 'twentyten' ); ?></h3> <ul> <?php wp_get_archives( 'type=monthly' ); ?> </ul> </li> <li> <h3><?php _e( 'Meta', 'twentyten' ); ?></h3> <ul> <?php wp_register(); ?> <li><?php wp_loginout(); ?></li> <?php wp_meta(); ?> </ul> </li> <?php endif; // end primary widget area ?> </ul>
-
Dan, this is what your after I think? I’ve added some extra comments, but not sure what you want to put in the else block.
Hope it helps
<?php
// This creates a sub navigation containing the child pages.
if ( is_page() ) { ?><?php
if($post->post_parent)
$children = wp_list_pages(‘title_li=&child_of=’.$post->post_parent.’&echo=0′); else
$children = wp_list_pages(‘title_li=&child_of=’.$post->ID.’&echo=0′);
if ($children) { ?><div class=”childnav”>
<h2>
<?php
$parent_title = get_the_title($post->post_parent);
echo $parent_title;
?>
</h2>-
<?php echo $children; ?>
</div>
<?php } // end if $children
} else {
// not page
} ?>- <?php get_search_form(); ?>
- <h3><?php _e( ‘Archives’, ‘twentyten’ ); ?></h3>
<?php
/* When we call the dynamic_sidebar() function, it’ll spit out
* the widgets for that widget area. If it instead returns false,
* then the sidebar simply doesn’t exist, so we’ll hard-code in
* some default sidebar stuff just in case.
*/
if ( ! dynamic_sidebar( ‘primary-widget-area’ ) ) : ?>-
<?php wp_get_archives( ‘type=monthly’ ); ?>
- <h3><?php _e( ‘Meta’, ‘twentyten’ ); ?></h3>
-
<?php wp_register(); ?>
- <?php wp_loginout(); ?>
<?php wp_meta(); ?>
<?php endif; // end primary widget area ?>
Thank you @freshsauce though WP seemed to ignore the if statement so I ended up using this:
<?php // This only displays the primary widgets if it is NOT a page // if ( ! is_page() ) { ?>I put that in front of the usual widget code which has worked!
The topic ‘Basic PHP script/syntax help in modifying sidebar’ is closed to new replies.