WordPress.org

Ready to get started?Download WordPress

Forums

Sort children by menu order (5 posts)

  1. tswartz
    Member
    Posted 4 years ago #

    I found this on the wp_list_pages template tag FAQ:

    <?php
    $children = wp_list_pages('title_li=&child_of=&sort_column=menu_order');
    if ($children) { ?>
    <ul>
    <?php echo $children; ?>
    </ul>
    <?php } ?>

    I modified it to sort by menu_order, which works for any parent pages, but not for the children. Any help is appreciated.

  2. MichaelH
    Member
    Posted 4 years ago #

    One alternative:

    <?php
    $args=array(
      'orderby' => 'menu_order',
      'order'=>'ASC',
      'post_type' => 'page',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Pages sorted in menu order';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  3. tswartz
    Member
    Posted 4 years ago #

    I need to have the children show up in their own unordered list (with each page in a list under the parent), which is also a list, like this:

    <ul>
    <li>Parent Page</li>
      <ul>
      <li>Child Page</li>
      </ul>
    <li>Another Page (not related to Parent Page)</li>
    </ul>

    Is that possible?

  4. MichaelH
    Member
    Posted 4 years ago #

    Not sure. Might look to see if the My Page Order plugin will help you.

  5. tswartz
    Member
    Posted 4 years ago #

    As an update for future readers, the original code I posted above actually does work; set the first child at 1 instead of 0 and it sorts fine.

Topic Closed

This topic has been closed to new replies.

About this Topic