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

时间:2018-01-14 14:24:46

标签: php wordpress dropdown

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

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

上述代码的网址:Displaying all pages in dropdown

提前致谢! :) 崔西

3 个答案:

答案 0 :(得分:1)

您必须添加参数

'orderby' => 'title', 'order' => 'ASC', // or DESC
请查找更新的代码。 。       

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

答案 1 :(得分:0)

您必须在WP_Query中添加排序

 $all_wp_pages = $my_wp_query->query( array(
      'post_type' => 'page',
      'posts_per_page' => -1,
      'orderby' => 'title',
 ));

答案 2 :(得分:0)

orderbyorder添加到查询数组中:

$all_wp_pages = $my_wp_query->query( array(
      'post_type' => 'page',
      'posts_per_page' => -1,
      'orderby' => 'title',
      'order'   => 'ASC', // or DESC
 ));