简体   繁体   中英

how to display custom posts types by title in a drop down menu in wordpress

i want to add a dropdown with the custom posts title, basically the code iam using shows default posts in a dropdown by title but i want to display a custom post type in a drop down any help plz here is my code

<select name="archive-dropdown"    
onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value=""><?php echo esc_attr( __( 'Select Location' ) ); ?></option> 
<?php wp_get_archives( array( 'type' => 'postbypost', 'format' => 'option',  
'show_post_count' => 1) ); ?>
 </select>  

try this

<?php
$type = 'products'; // your post type
$args=array(
  'post_type' => $type,
  '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() ) {
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <option><?=the_title(); ?></option>
    <?php
  endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM