如何在wordpress的下拉菜单中按标题显示自定义帖子类型

时间:2014-06-05 05:30:10

标签: php wordpress

我想添加一个带有自定义帖子标题的下拉列表,基本上代码iam使用在标题下拉列表中显示默认帖子但我想在下拉列表中显示自定义帖子类型任何帮助下载这里是我的代码

<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>  

1 个答案:

答案 0 :(得分:1)

试试这个

<?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().
?>