Support » Fixing WordPress » If/Else Statement for if a page has child pages?

  • Resolved leesus.

    (@leesus-1)


    I’m currently working on a site using WP as a cms and I’ve hit a roadblock. I’m using main parent pages to show product (child) pages as thumbnails. This works ok, the problem is I’ve got a special offers page that may not have child pages. I want to test if the page has children, if it does show the thumbnails as it is currently, if not display a generic “sorry we don’t have anything on offer” message.

    Anyone know if I can test if it the page has child pages or not (sorry, PHP isn’t my strong point!)

    Thanks in advance

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

    (@esmi)

    Forum Moderator

    How about using <?php $children = get_pages('child_of='.$post->ID);?>

    This will list children & grandchildren. If you only want the immediate children, use <?php $children = get_pages('child_of='.$post->ID.'&parent='.$post->ID);?>

    http://codex.wordpress.org/Function_Reference/get_pages

    Thanks esmi, I don’t think thats what I’m after though.

    I’ve already got the template to show a list with thumbnails of the page’s child pages. What I need is a way of displaying a different message if there are no child pages, or listing them in the way I am currently if there are.

    Example of what I mean: if ($page_has_children) { show my list as normal } else { show “no offers” text }

    How could I do the $page_has_children test to then write a statement around?

    Hope that makes sense!

    esmi

    (@esmi)

    Forum Moderator

    <?php
    $children = get_pages('child_of='.$post->ID);?>
    if( count( $children ) != 0 ) { show list as normal }
    else { show "no offers" text }
    ?>

    @esmi:great…thanks a lot

    @esmi
    Thanks, this helped me too.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘If/Else Statement for if a page has child pages?’ is closed to new replies.