mrc2407
Member
Posted 2 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? :)
AnttiJN
Member
Posted 2 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 }
}
?>
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
);
mrc2407
Member
Posted 2 years ago #
Hey, I used vtxyzzy's code and it works nice! :) Thank you! Thank you too, MichaelH :)
sorry mine was pretty much similar code as already posted above
Glad you got it working. Now, please use the dropdown at top right to mark this topic 'Resolved'.
JGrizzLee
Member
Posted 1 year 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?