WordPress.org

Ready to get started?Download WordPress

Forums

sub pages in a jump menu (8 posts)

  1. gjuddy
    Member
    Posted 5 years ago #

    I'm using the following code to display a list of child pages within a child page...

    <?php
      if($post->post_parent)
      $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
      else
      $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
      if ($children) { ?>
      <ul>
      <?php echo $children; ?>
      </ul>
      <?php } ?>

    It works great but I'm wondering how to adjust it so it shows up as a jump menu instead of an ordered list.

    Any help would be much appreciated.

  2. gjuddy
    Member
    Posted 5 years ago #

    ok, I've figured out how to list the child pages as a select menu instead of a list...(quite easy, just change wp_list_pages to wp_dropdown_pages).

    So now I've got this...

    <?php
      if($post->post_parent)
      $children = wp_dropdown_pages("title_li=&child_of=".$post->post_parent."&echo=0");
      else
      $children = wp_dropdown_pages("title_li=&child_of=".$post->ID."&echo=0");
      if ($children) { ?>
      <ul>
      <?php echo $children; ?>
      </ul>
      <?php } ?>

    Can anyone tell me how to turn this list into a jump menu...so when any one of the items in the list are selected, you are forwarded to that page.

  3. gjuddy
    Member
    Posted 5 years ago #

    So now I've got it figured out...

    <form action="<?php bloginfo('url'); ?>/" method="get">
    <?php
    if($post->post_parent)
    $select = wp_dropdown_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    else
    $select = wp_dropdown_pages("title_li=&child_of=".$post->ID."&echo=0");
    $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
    echo $select;
    ?>
    </form>

    The above code pulls all the sub pages with the same parent as the live page and puts them in a jump menu.

    Can anyone tell me how to utilize the "selected" parameter to have the menu default to whatever page is live. Currently it always just lists the first item in the menu...

  4. 216cro
    Member
    Posted 5 years ago #

    Don't know if this helps, but I used the following code...

    <form action="<?php bloginfo('url'); ?>" method="get">
    <?php
    echo '<!-- ';
    $select = wp_dropdown_pages('sort_column=menu_order');
    echo ' -->';
    $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
    echo $select;
    ?>
    </form>

    I couldn't stop the first instance of wp_dropdown_pages printing the form so I used comment tags either side. So it doesn't show on the page, but does in the source code. Oh well...

  5. 216cro
    Member
    Posted 5 years ago #

    It still just lists the first item in the menu...

  6. ensignkid
    Member
    Posted 5 years ago #

    I'm having the exact same issue. I've tried adding this line of code which will appear at the top of the list. I'm using a dual dropdown though.

    $select = wp_dropdown_pages('sort_column=menu_order&show_option_none=Select a Topic');

    In the above example, "Select a Topic" appears at the top of the list and is shown.

    I've tried replacing with "Select a Topic" with a bunch of other variables to display the current page title.

    '.$page.'

    Or something like that should work, but I haven't figured it out out.

  7. ensignkid
    Member
    Posted 5 years ago #

    All you have to do is to re-order the list to force the current page to appear first or use the show_option_none code.

  8. gjuddy
    Member
    Posted 5 years ago #

    Finally got it figured out...

    <form action="<?php bloginfo('url'); ?>/" method="get">
    <?php
    if($post->post_parent)
    $select = wp_dropdown_pages("sort_column=menu_order&selected=".$post->ID."&title_li=&child_of=".$post->post_parent."&echo=0");
    else
    $select = wp_dropdown_pages("title_li=&child_of=".$post->ID."&echo=0");
    $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
    echo $select;
    ?>
    </form>

    Thanks ensignkid for the sort column tip as well.

Topic Closed

This topic has been closed to new replies.

About this Topic