WordPress.org

Forums

[resolved] Nested PHP if statements (6 posts)

  1. kalico
    Member
    Posted 6 years ago #

    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?

  2. Roger Theriault
    Member
    Posted 6 years ago #

    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".

  3. kalico
    Member
    Posted 6 years ago #

    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!>

  4. kalico
    Member
    Posted 6 years ago #

    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..........

  5. kalico
    Member
    Posted 6 years ago #

    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; 
    
    }
    }
    ?>
  6. Roger Theriault
    Member
    Posted 6 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.