Back WordPress

How to Group Articles or Posts Alphabetically

WRITTEN BY ON 14 Feb 2011
8,643 VIEWS • SHARES
0 comments

To help your site visitors quickly find one or two posts, you can create a list post ordered by date, category, or alphabet. There are tons of tutorials you can find to create this list. But this tutorial is an advance version to list post ordered by alphabet visit for more: http://www.dynamicwp.net/articles-and-tutorials/how-to-group-articles-or-posts-alphabetically/

PHP

<?php
$args=array(
  'orderby' => 'title',
  'order' => 'ASC',
  'posts_per_page'=>-1,
  'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  echo 'Alphabetic index of all ' . count($my_query->posts) . ' posts';
  while ($my_query->have_posts()) : $my_query->the_post();
    $this_char = strtoupper(substr($post->post_title,0,1));
    if ($this_char != $last_char) {
      $last_char = $this_char;
      echo '<h2>'.$last_char.'</h2>';
    } ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    <?php
  endwhile;
}
wp_reset_query();
?>

Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.