WordPress.org

Forums

[resolved] If/Else Statement for if a page has child pages? (6 posts)

  1. leesus.
    Member
    Posted 5 years ago #

    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

  2. esmi
    Forum Moderator
    Posted 5 years ago #

    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

  3. leesus.
    Member
    Posted 5 years ago #

    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!

  4. esmi
    Forum Moderator
    Posted 5 years ago #

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

    @esmi:great...thanks a lot

  6. maark0_o
    Member
    Posted 4 years ago #

    @esmi
    Thanks, this helped me too.

Topic Closed

This topic has been closed to new replies.

About this Topic