Title: Nested PHP if statements
Last modified: August 19, 2016

---

# Nested PHP if statements

 *  Resolved [kalico](https://wordpress.org/support/users/kalico/)
 * (@kalico)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/nested-php-if-statements/)
 * I know this php code is a mess, but I”m not skilled enough in php to fix it. 
   I think what I’m trying to do is nest two if statements. The inner one works 
   fine on it’s own, but I only want it to run “if” it’s NOT on a child page.
 *     ```
       <?php
       if (is_page() && $post->post_parent ) {
          // do not display page list
       } else {
   
       if($post->post_parent)
       $children = wp_list_pages("title_li=&depth=1&child_of=".$post->post_parent."&echo=0"); else
       $children = wp_list_pages("title_li=&depth=1&child_of=".$post->ID."&echo=0");
       if ($children) { ?>
       <ul>
       <?php echo $children; ?>
       </ul>
   
       }
       ?>
       ```
   
 * What I want it to do, in layman’s terms is this:
 * if this is a page, AND it has a parent (it is a child)
    — display nothing else—
   run the next little chunk of php code, which works fine independent of the outer
   if statement.
 * Any help?

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

 *  [Roger Theriault](https://wordpress.org/support/users/rogertheriault/)
 * (@rogertheriault)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/nested-php-if-statements/#post-801623)
 * You just need another curly brace at the end so they match. Which the syntax 
   checker in PHP probably will tell you about when the code is run.
 * I hope you don’t mind if I recommend you research PHP structure via the resources
   at php.net? The IF statement’s variations are described there and it’s easy to
   search… actually Google usually pulls up php.net in the first results if you 
   make a query such as “php if”.
 *  Thread Starter [kalico](https://wordpress.org/support/users/kalico/)
 * (@kalico)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/nested-php-if-statements/#post-801658)
 * Hi Roger, I really appreciate the tip. I did some research there, but so far 
   I have run into the same problem I usually do — there isn’t anything (that I 
   can find) that applies specifically to my situation. I couldn’t find anything
   on nesting “if” statements, although I found plenty on ifs in general, but really
   basic stuff. I can handle basic…this seems to be at a level of complexity not
   dealt with in standard tutorials.
 * The following code is what I have now, but it’s still not working. I get an error
   about an unexpected ‘<‘ which is probably the
    `<ul>`. Any helpful hints would
   be greatly appreciated.
 *     ```
       <?php
       if (is_page() && $post->post_parent ) {
          // do not display page list
       } else {
   
       if($post->post_parent)
       $children = wp_list_pages("title_li=&depth=1&child_of=".$post->post_parent."&echo=0");
       else
       $children = wp_list_pages("title_li=&depth=1&child_of=".$post->ID."&echo=0");
       if ($children) {
       <ul>
       <?php echo $children;
       </ul>
   
       }
       }
       ?>
       ```
   
 * I added the curly bracket, but those really confuse me — because the chunk of
   code that works on it’s own doesn’t have many curly brackets – just at the beginning
   and end. The other if/else parts don’t have them. And yet all the tutorials say
   they are necessary. <confused!>
 *  Thread Starter [kalico](https://wordpress.org/support/users/kalico/)
 * (@kalico)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/nested-php-if-statements/#post-801661)
 * Whoa! I fixed it!! Well, part of it. At least the page isnt’ broken anymore. 
   It was the `<?php` stuck in the middle of it. Duh.
 * Ok so the code is clean and it works now, BUT….it still doesn’t do what I want
   it to do. I described that in the OP, so if anyone can assist, that would be 
   super. I’ll keep working on it too……….
 *  Thread Starter [kalico](https://wordpress.org/support/users/kalico/)
 * (@kalico)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/nested-php-if-statements/#post-801663)
 * Ha!! it works!! I thought it didn’t work, but it does. I just had some other 
   stuff in there that was throwing me off. Anyway, for the record, here is the 
   WORKING code. And thanks again Roger!!
 *     ```
       <?php
       if (is_page() && $post->post_parent ) {
          // do not display page list
       } else {
   
       if($post->post_parent) {
       $children = wp_list_pages("title_li=&depth=1&child_of=".$post->post_parent."&echo=0");
       } else {
       $children = wp_list_pages("title_li=&depth=1&child_of=".$post->ID."&echo=0");
       } if ($children) { 
   
       echo $children; 
   
       }
       }
       ?>
       ```
   
 *  [Roger Theriault](https://wordpress.org/support/users/rogertheriault/)
 * (@rogertheriault)
 * [17 years, 11 months ago](https://wordpress.org/support/topic/nested-php-if-statements/#post-801720)
 * If you need the ul tags around the children (to validate the xhtml properly, 
   for example), it would go like this:
 *     ```
       } if ($children) {
       ?>
       <ul>
       <?php echo $children; ?>
       </ul>
       <?php
       }
       ```
   
 * And if you plan to do much more PHP, you really ought to spend $25 on a beginning
   php book (the O’Reilly ones are excellent). PHP is not something you can just
   cut and paste from examples and hope to work… once you have an understanding 
   of what you’re doing you’ll be able to do a lot more for your site… and this 
   forum really isn’t a place for fixing php syntax issues.

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

The topic ‘Nested PHP if statements’ is closed to new replies.

 * 5 replies
 * 2 participants
 * Last reply from: [Roger Theriault](https://wordpress.org/support/users/rogertheriault/)
 * Last activity: [17 years, 11 months ago](https://wordpress.org/support/topic/nested-php-if-statements/#post-801720)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
