How to Group Articles or Posts Alphabetically
14th Feb 2011 | Posted by reza | 15 CommentsTo 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
The post in this list will be grouped by it’s alphabet, so the list will be like this:
A.
Afirst post
Alorem Ipsum
B.
Bolot sit amet
Bconsectetur adipisicing
etc..etc
How to
Ok, here’s the code :
<?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(); ?>
You can put this code in a page template to create a site map.
Hope this code helps.
Credit:
Thanks to khamtv and MichaelH from WordPress.org
Save your time and get on time success in 6401-1 exams by using our latest 642-902 and other superb exam pass resources of 70-642.
13 Comments to “How to Group Articles or Posts Alphabetically”
Add Comments (+)-
Thanks for your help!
PS. How can I get the first letter was UTF-8, not a strange sign? -
<?php $args=array( 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page'=>-1, 'caller_get_posts'=>1, 'post_type' => 'autorzy' ); $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(mb_substr($post->post_title,0,1,'UTF-8')); 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(); ?>
-
Show me this list:
A
D
K
P
W
K
P
ŚWhat is wrong?
-
How to show only posts that starts with, for example, the letter C ?
-
Hello,
Thanks for the helpful code. I was wondering if it would be possible to sort and group by a custom field value instead of by post title?
Is it possible to modify this line:
$this_char = strtoupper(mb_substr($post->post_title,0,1,’UTF-8′));
With something instead about a custom field value? I tried modifying the query args to get meta values and orderby meta value, but it messed up the order below.
Thanks!
-
Hi,
I wondered if i could add an rule :
when the article starts with The, then use the first letter of the second word.
Example:
if the title of the article is “The Article”
instead of appearing at letter T, appear at letter AAny help?
Thx
Daniel
Trackbacks/Pingbacks
- How to Group Articles or Post by Author
- 70 Excellent WordPress Tips And Tutorials From First Three Months Of 2011
That was a fast and nice tip! thx for that one… keep em coming!