donglun1020 2018-02-19 11:18
浏览 83
已采纳

如何显示wordpress中的所有帖子,我正在使用分页,所以它显示的帖子显示为每页没有其他导航其他页面

Any WordPress experts, Please Help me.

I am trying to display all posts in the drop-down menu, I get result 12 results of drop-down options, but another second page (I am using pagination) post is not shown in the drop down menu, when I go to the second page I show that page posts dropdown result. I want to show all posts in the dropdown menu on every page pages.

Code:-

<?php 

 wp_list_pages(array('post_type'=>'brands')); 

?> 
<select class="form-control pdt-category-select search-space" id="brands" name="brands"> 
   <option>All</option> 
   <?php foreach ($posts as $post) { 
     echo '<option value="', $post->post_name, '"', $selected == $post->ID ? ' selected="selected"' : '', '>', $post->post_title, '</option>'; 
   }?> 
</select>
  • 写回答

1条回答 默认 最新

  • douzi1991 2018-02-19 12:02
    关注

    Here you called function but you didn't assign return value to any variable:

    wp_list_pages(array('post_type'=>'brands'));
    

    Edit it to:

    $dropdown_posts = wp_list_pages(array('post_type'=>'brands'));
    

    Function wp_list_pages is returning string in HTML format https://developer.wordpress.org/reference/functions/wp_list_pages/. You don't need to loop it, just echo it:

    <?php echo $dropdown_posts ?>
    

    If you rather loop through posts use function get_posts() like this:

    <?php 
    
     $dropdown_posts = get_posts(array('post_type'=>'brands')); 
    
    ?> 
    <select class="form-control pdt-category-select search-space" id="brands" name="brands"> 
       <option>All</option> 
       <?php foreach ($dropdown_posts as $post) { 
         echo '<option value="', $post->post_name, '"', $selected == $post->ID ? ' selected="selected"' : '', '>', $post->post_title, '</option>'; 
       }?> 
    </select>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?