WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Sort archives alphabetically grouped by starting letter (8 posts)

  1. mrc2407
    Member
    Posted 3 years ago #

    Hi!

    I've created a page template that shows all the posts from a custom post type, and I would like to sort them alphabetically and grouped by the starting letter. For example:

    #A#
    An example
    Another example

    #C#
    Custom post types are cool

    #W#
    Wow!

    Any idea on how to do this? :)

  2. anttijn
    Member
    Posted 3 years ago #

  3. vtxyzzy
    Member
    Posted 3 years ago #

    This should get you started:

    <?php
       $args = array(
         'orderby' => 'title',
         'order' => 'ASC',
         'caller_get_posts' => 1,
         'posts_per_page' => 20,
                     );
    query_posts($args);
    if (have_posts()) {
      $curr_letter = '';
      while (have_posts()) {
        the_post();
        $this_letter = strtoupper(substr($post->post_title,0,1));
        if ($this_letter != $curr_letter) {
          echo "<h2> # $this_letter #</h2>";
          $curr_letter = $this_letter;
        }
    ?>
    <br />
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
      <?php }
    }
    ?>
  4. MichaelH
    Member
    Posted 3 years ago #

    http://wordpress.org/support/topic/plugin-list-category-posts-how-to-group-articles-in-all-categories-by-the-a-b-c-groups?replies=4

    Just change the query args

    $args=array(
      'post_type' => 'your custom post type here',
      'orderby' => 'title',
      'order' => 'ASC',
      'posts_per_page'=>-1,
      'caller_get_posts'=>1
    );
  5. mrc2407
    Member
    Posted 3 years ago #

    Hey, I used vtxyzzy's code and it works nice! :) Thank you! Thank you too, MichaelH :)

  6. Tara
    Member
    Posted 3 years ago #

    sorry mine was pretty much similar code as already posted above

  7. vtxyzzy
    Member
    Posted 3 years ago #

    Glad you got it working. Now, please use the dropdown at top right to mark this topic 'Resolved'.

  8. JGrizzLee
    Member
    Posted 3 years ago #

    This works great, but is there any way to link to a certain letter? For example, if there was an A-Z list and you clicked on C, it would jump down to where the C's start. Or if you clicked on Z it would take you to the page that Z starts on?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags