Title: Basic PHP script/syntax help in modifying sidebar
Last modified: August 19, 2016

---

# Basic PHP script/syntax help in modifying sidebar

 *  Resolved [morgyface](https://wordpress.org/support/users/morgyface/)
 * (@morgyface)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/basic-php-scriptsyntax-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>
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [freshsauce](https://wordpress.org/support/users/freshsauce/)
 * (@freshsauce)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/basic-php-scriptsyntax-help-in-modifying-sidebar/#post-1866991)
 * 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
       /* 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 get_search_form(); ?>
    -  <h3><?php _e( ‘Archives’, ‘twentyten’ ); ?></h3>
 *  -  <?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 ?>
 *  Thread Starter [morgyface](https://wordpress.org/support/users/morgyface/)
 * (@morgyface)
 * [15 years, 4 months ago](https://wordpress.org/support/topic/basic-php-scriptsyntax-help-in-modifying-sidebar/#post-1867011)
 * Thank you [@freshsauce](https://wordpress.org/support/users/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!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Basic PHP script/syntax help in modifying sidebar’ is closed to new replies.

## Tags

 * [else](https://wordpress.org/support/topic-tag/else/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [script](https://wordpress.org/support/topic-tag/script/)
 * [sidebar](https://wordpress.org/support/topic-tag/sidebar/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [morgyface](https://wordpress.org/support/users/morgyface/)
 * Last activity: [15 years, 4 months ago](https://wordpress.org/support/topic/basic-php-scriptsyntax-help-in-modifying-sidebar/#post-1867011)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
