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

  • 写回答

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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?