Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello,

    You can try something similar to the following (Untested!) code:

    <select name="page-dropdown"
     onchange='document.location.href=this.options[this.selectedIndex].value;'> 
     <option value="">
    <?php echo esc_attr( __( 'Select page' ) ); ?></option> 
     <?php 
      $pages = get_pages(); 
      foreach ( $pages as $page ) {
      	$option = '<option value="' . get_page_link( $page->ID ) . '">';
    	$option .= $page->post_title;
    	$option .= '</option>';
    	echo $option;
      }
     ?>
    </select>

    The above will get you all pages and upon selection send you to that page. To only show certain pages in this list just add in some arguments into:

    $pages = get_pages($args);

    Here you can list pages by page ID:

    'page_id' => 7

    or page slug:

    'pagename' => 'contact'

    As for the submit button, what are you trying to achieve? Redirecting a user as soon as they’ve selected a page from the dropdown would mean less clicks than having to click ‘submit’ to go the page after selection.

    ~ Steven

    Thread Starter Md Sadiqur Rahman

    (@sadiq20161)

    Thank you so much for your kind reply. I am so sorry to say that, I do not know coding. Let say, I want to get all the child pages of a page (with id say “3”) in a drop down list. I will be really happy, if I get a straight forward code to copy-paste.

    Thank you

    If you don’t code then you’re probably better off looking for a plugin that uses a shortcode to display child pages. Something like this might be useful: https://en-gb.www.ads-software.com/plugins/child-pages-shortcode/ however it doesn’t put them in a dropdown.

    Using a code snippet like the one above you would have to be comfortable adding the code into the template files. Is this something you can do?

    ~ Steven

    Thread Starter Md Sadiqur Rahman

    (@sadiq20161)

    Thank you so much for your support. I got the following code to get child pages. Putting the shortcode [wpb_childpages] gives me child pages of the current page. It does not give dropdown list though, I solved my problem manually.

    function wpb_list_child_pages() { 
    global $post; 
    if ( is_page() && $post->post_parent )
    	$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
    else
    	$childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
    if ( $childpages ) {
    	$string = '<ul>' . $childpages . '</ul>';
    }
    return $string;
    }
    add_shortcode('wpb_childpages', 'wpb_list_child_pages');
    

    Thank you so much for your kind response.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get drop downlist of “certain” pages’ is closed to new replies.