chihung
7/9/2019 - 5:28 AM

list all page with parent and its child (sub page)

list all page with parent and its child (sub page)

<ul>
    <?php
    if($post->post_parent){
        $children = get_pages("child_of=".$post->post_parent);
        $parent_title = get_the_title($post->post_parent);
        $link = get_permalink($post->post_parent);
    }
    else{
        $children = get_pages("child_of=".$post->ID);
        $parent_title = get_the_title($post->ID);
        $link = get_permalink($post->ID);
        $parent_page = $post->ID;
    }
    if ($children) {
        ?>
        <li <?php if( !empty($parent_page) && $parent_page==$post->ID ){echo 'class="current-menu-item"';} ?>><a href="<?php echo $link; ?>"><?php echo $parent_title;?></a></li>
        <?php
        foreach( $children as $post ) : setup_postdata($post);
            ?>
            <li <?php if(is_page($post->ID)){echo 'class="current-menu-item"';} ?>>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </li>
        <?php
        endforeach;
        ?>
        <?php
    }

    else  {
        ?>
        <li><a href="<?php echo $link; ?>"><?php echo $parent_title;?></a></li>

        <?php
    }
    ?>


</ul>