2

I have a custom post type, Doctors, that I need to create a dropdown nav for. I just want it to populate a select list with all the posts of that CPT and navigate to that post on selection.

I'm doing a couple other dropdowns with wp_dropdown_categories, but I guess there's no built in function for listing a post type?

6

You'll need to use get_posts and roll your own drop down.

Something like this (somewhere in functions.php):

<?php
function wpse34320_type_dropdown( $post_type )
{
    $posts = get_posts(
        array(
            'post_type'  => $post_type,
            'numberposts' => -1
        )
    );
    if( ! $posts ) return;

    $out = '<select id="wpse34320_select"><option>Select a Doctor</option>';
    foreach( $posts as $p )
    {
        $out .= '<option value="' . get_permalink( $p ) . '">' . esc_html( $p->post_title ) . '</option>';
    }
    $out .= '</select>';
    return $out;
}

Then in your template...

<?php echo wpse34320_type_dropdown( 'doctors' ); ?>
  • Thanks so much! I'll try that out. I need to finally learn PHP... – Holden Nov 21 '11 at 13:10
  • Can't seem to get this working - any suggestions? I'm not even seeing a dropdown... – Holden Nov 21 '11 at 19:55
0

This function behaves more like wp_dropdown_categories

function

public function wpse34320_get_dropdown_posts( $args = array( 'post_type' => 'post', 'show_option_none'  => 'Select a post', 'name' => null, 'selected' => '', 'echo' => true ) ){

    $posts = get_posts(
        array(
            'post_type'  => $args['post_type'],
            'numberposts' => -1
        )
    );

    $dropdown = '';

    if( $posts ){

        if( !is_string($args['name']) ){

            $args['name'] = $args['post_type'].'_select';
        }

        $dropdown .= '<select id="'.$args['name'].'" name="'.$args['name'].'">';

            $dropdown .= '<option value="-1">'.$args['show_option_none'].'</option>';

            $args['selected'] = intval($args['selected']);

            foreach( $posts as $p ){

                $selected = '';
                if( $p->ID == $args['selected'] ){

                    $selected = ' selected';
                }

                $dropdown .= '<option value="' . $p->ID . '"'.$selected.'>' . esc_html( $p->post_title ) . '</option>';
            }

        $dropdown .= '</select>';           
    }

    if($args['name'] === false){

        return $dropdown;
    }
    else{

        echo $dropdown;
    }
}

use

   $args=array(

            'show_option_none'  => 'Select a custom post type',
            'post_type'         => $post_type,
            'name'              => $name,
            'selected'          => $selected_id,
            'echo'              => true
   );

    wpse34320_get_dropdown_posts($args);    
-1

I've added a javascript line to 'Jump' directly to the selection.

Before:

$out = '<select id="wpse34320_select"><option>Select a Doctor</option>';

After:

$out = '<form name="jump1"><select name="jump2" id="wpse34320_select" OnChange="location.href=jump1.jump2.options[selectedIndex].value">><option>Select a doctor</option>';
  • 2
    This is really, really annoying for keyboard users. They cannot reach the second page now. – fuxia Feb 5 '12 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.