Wordpress php list all pages alphabetically

前端 未结 3 1175
生来不讨喜
生来不讨喜 2021-01-27 13:56

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?

相关标签:
3条回答
  • 2021-01-27 14:27

    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',
     ));
    
    0 讨论(0)
  • 2021-01-27 14:32

    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>
    
    0 讨论(0)
  • 2021-01-27 14:41

    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
     ));
    
    0 讨论(0)
提交回复
热议问题