Display Custom Post Types Glossary

I used this code to create a glossary for a custom pos called “store”.
So you can use it just replacing the “store” by “book”. Put this code in the loop of the page template you will be using for the glossary.


$last_char="";
$args=array(
  'orderby' => 'title',
  'order' => 'ASC',
  'posts_per_page'=>-1,
  'post_type' => 'store'
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  echo count($my_query->posts) . ' магазинов';
  echo '';
  while ($my_query->have_posts()) : $my_query->the_post();

$this_char = mb_strtoupper(mb_substr($post->post_title,0,1));
if ($this_char != $last_char) {
  $last_char = $this_char;
  echo '<a href="#'.$last_char.'">'.$last_char.'</a>';
}

endwhile;
while ($my_query->have_posts()) : $my_query->the_post();
$this_char = mb_strtoupper(mb_substr($post->post_title,0,1));
if ($this_char != $last_char) {
$last_char = $this_char;
echo ”.$last_char.”;
}
echo ”. get_the_title() .”;

endwhile;
}
wp_reset_query();