Question

I have a WordPress website where I am trying to put the menus dynamically on sidebar based on Parent Page as follows subpages under the parent Page. For example:

EXMAPLE -Example 1 -Example 2 -Example 3

when I select the attribute on a page, the sidebar menu should look something like above.

Well I have done this, and every links are showing up but the only problem is order. The order of pages on menus are not following the order or their IDs. > http://demo.chilipress.com/clinic/?page_id=74 Please take a look on the sidebar. For example "medical staff" is showing at the bottom, but it should show just after "about the line" since its page ID is "74". I think you have understood the problem. I have used the code below:

                            <ul class="side-nav">
                            <?php if(is_page('$post->post_parent')): ?><?php endif; ?>
                            <li <?php if(is_page($post->post_parent)): ?>class="current_page_item"<?php endif; ?>><a href="<?php echo get_permalink($post->post_parent); ?>" title="Back to Parent Page"><?php echo get_the_title($post->post_parent); ?></a></li>
                            <?php
                            if($post->post_parent)
                            $children = get_pages(array("child_of" => $post->post_parent));
                            else
                            $children = get_pages(array("child_of" => $post->ID));
                            if ($children) {
                            ?>
                            <?php 

                                foreach($children as $c):
                                    ?>
                                    <li class="page_item page-item-<?php echo $c->ID ?> <?php if($c->ID == get_the_ID()): ?>current_page_item<?php endif; ?>"><a href="<?php echo get_permalink($c->ID) ?>"><?php echo get_the_title($c->ID) ?></a></li>
                                    <?php
                                endforeach;

                             ?>
                            <?php } ?>
                        </ul>

Can anyone tell me what should I include with this code to rearrange my menu automatically so that they appears in a order of page IDs?

Was it helpful?

Solution

You can check the codex for the function you are using, namely - get_pages:
https://codex.wordpress.org/Function_Reference/get_pages

You need to include 'sort_column' => 'ID'.
An example using your code:

$children = get_pages( array(
    'child_of'    => $post->post_parent,
    'sort_column' => 'ID'
) );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top