【问题标题】:how to display custom posts types by title in a drop down menu in wordpress如何在wordpress的下拉菜单中按标题显示自定义帖子类型
【发布时间】:2016-04-01 15:57:51
【问题描述】:

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

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

【问题讨论】:

    标签: php wordpress


    【解决方案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().
    ?>
    

    【讨论】:

      猜你喜欢
      • 2020-05-11
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      相关资源
      最近更新 更多