dongzu3511
2018-01-14 14:24
浏览 41
已采纳

Wordpress php按字母顺序列出所有页面

I found this code on here that pulls all wordpress pages and displays them in a dropdown list. My question is what needs to be changed to have these pages listed alphabetically?

<form action="">
<select>

<?php // Query for listing all pages in the select box loop
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query( array(
'post_type' => 'page',
'posts_per_page' => -1
));

foreach ($all_wp_pages as $value){
$post = get_page($value);
$title = $post->post_title;
$id = $post->ID;

// For example
// <option value="pageId32">Page title</option>

echo '<option value="pageId' . $id. '">' . $title . '</option>';

}; ?>

</select>

URL of above code: Displaying all pages in dropdown

Thanks in Advance! :) Tracy

图片转代码服务由CSDN问答提供 功能建议

我在这里找到了这个代码,它可以提取所有wordpress页面并在下拉列表中显示它们。 我的问题是需要更改哪些内容按字母顺序列出这些页面?

 &lt; form action =“”&gt; 
&lt; select&gt; 
 
&lt;?php  //查询列出选择框循环中的所有页面
 $ my_wp_query = new WP_Query(); 
 $ all_wp_pages = $ my_wp_query-&gt; query(array(
'post_type'=&gt;'page',
  'posts_per_page'=&gt; -1 
)); 
 
foreach($ all_wp_pages as $ value){
 $ post = get_page($ value); 
 $ title = $ post-&gt; post_title; 
  $ id = $ post-&gt; ID; 
 
 //例如
 //&lt; option value =“pageId32”&gt;页面标题&lt; / option&gt; 
 
echo'&lt; option value =“pageId  '。$ id。'“&gt;'  。  $ title。  '&LT; /选项&GT;'; 
 
};  ?&gt; 
 
&lt; / select&gt; 
   
 
 

上述代码的网址:在下拉列表中显示所有页面

提前致谢! :) Tracy

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doufeiqiong3515 2018-01-14 14:39
    已采纳

    You have to add parameters

    'orderby' => 'title', 'order' => 'ASC', // or DESC
    Please find updated code. .

    <?php // Query for listing all pages in the select box loop
    $my_wp_query = new WP_Query();
    $all_wp_pages = $my_wp_query->query( array(
    'post_type' => 'page',
    'posts_per_page' => -1,
     'orderby' => 'title',
     'order'   => 'ASC', // or DESC
    ));
    
    foreach ($all_wp_pages as $value){
    $post = get_page($value);
    $title = $post->post_title;
    $id = $post->ID;
    
    // For example
    // <option value="pageId32">Page title</option>
    
    echo '<option value="pageId' . $id. '">' . $title . '</option>';
    
    }; ?>
    
    </select>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doucu5836 2018-01-14 14:29

    You have to add sorting in WP_Query

     $all_wp_pages = $my_wp_query->query( array(
          'post_type' => 'page',
          'posts_per_page' => -1,
          'orderby' => 'title',
     ));
    
    评论
    解决 无用
    打赏 举报
  • dsf55s1233 2018-01-14 14:30

    add orderby and order to query array:

    $all_wp_pages = $my_wp_query->query( array(
          'post_type' => 'page',
          'posts_per_page' => -1,
          'orderby' => 'title',
          'order'   => 'ASC', // or DESC
     ));
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题