0

I have this code:

<?php

    global $post;

    $children = get_pages( array( 'child_of' => $post->ID ) );

    if ( is_page() && $post->post_parent > 0 )
    { 
        // Is subpage
        // Here I want to get all sibling from the current page
    } 
    else 
    {
        // Is no subpage
        if( count( $children ) == 0 ) 
        {
            // Has no children
        } 
        else 
        {
            // Has children
            echo '<div class="subnav-header">';
            echo '<div class="subnav-table">';
            echo '<ul>';
            foreach($children as $child){ ?>
                <li><a href="<?php echo $child->guid; ?>"><?php echo $child->post_title; ?></a></li>
            <?php }
            echo '</ul>';
            echo '</div>';
            echo '</div>';
        }

    }

?>

First, it detects if the current page is a subpage, or normal page. When the page is a subpage, I want to display all the sibling pages of the current page just like I did if the page is no subpage, and has children.


So, in short, what I want to do is this:

  • When the current page is a subpage, check if the page has any siblings.
  • If the page has sibling, display them in a foreach loop.
1

Solved it, and decided I could best post my answer here, since people can learn from it :)

<?php


    global $post;

    $children = get_pages( array( 'child_of' => $post->ID ) );
    $siblings = get_pages( array( 'child_of' => $post->post_parent ) );

    // or, if you want to exclude the current page:
    // $siblings = get_pages( array( 'child_of' => $post->post_parent, 'exclude' => $post->ID ) );

    if ( is_page() && $post->post_parent > 0 )
    { 
        // Is subpage
        if( count( $siblings ) == 0 )
        {
            // Has no siblings
        }
        else
        {
            // Has siblings
            echo '<div class="subnav-header">';
            echo '<div class="subnav-table">';
            echo '<ul>';
            foreach($siblings as $sibling){ ?>
                <li><a href="<?php echo $sibling->guid; ?>"><?php echo $sibling->post_title; ?></a></li>
            <?php }
            echo '</ul>';
            echo '</div>';
            echo '</div>';
        }
    } 
    else 
    {
        // Is no subpage
        if( count( $children ) == 0 ) 
        {
            // Has no children
        } 
        else 
        {
            // Has children
            echo '<div class="subnav-header">';
            echo '<div class="subnav-table">';
            echo '<ul>';
            foreach($children as $child){ ?>
                <li><a href="<?php echo $child->guid; ?>"><?php echo $child->post_title; ?></a></li>
            <?php }
            echo '</ul>';
            echo '</div>';
            echo '</div>';
        }

    }

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.