Revision 340 is a pre-publication revision. (Viewing current revision instead.)

Sort WordPress archives alphabetically grouped by starting letter

I know of no plugin that does this. However, some custom coding displaying every letter regardless of having posts or not: http://wordpress.pastebin.com/f524c9f4 [php]<?php // Reference: http://wordpress.org/support/topic/148727 $posts = new WP_Query("cat=$cat&orderby=title&order=ASC&showposts=-1"); if ($posts->have_posts()) : for($i='A';$i!='AA';$i++) : ?> <h3 id="<?php echo $i; ?>"></a><?php echo $i; ?></h3> <ul> <?php while ($posts->have_posts()) : $posts->the_post(); if( $i == strtoupper(trim(substr($post->post_title, 0, 1))) ) : ?> <li><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php endif; endwhile; ?> </ul> <?php endfor; endif; ?>[/php] Version that only displays letter heading when one or more posts fall under it: http://wordpress.pastebin.com/f2302b7cb [php]<?php // Reference: http://wordpress.org/support/topic/148727 $posts = new WP_Query("cat=$cat&orderby=title&order=ASC&showposts=-1"); if ($posts->have_posts()) : for($i='A';$i!='AA';$i++) : ?> <?php while ($posts->have_posts()) : $posts->the_post(); if( $i == strtoupper(trim(substr($post->post_title, 0, 1))) ) : if( !$a_z_header ) : $a_z_header = 1; ?> <h3 id="<?php echo $i; ?>"></a><?php echo $i; ?></h3> <ul> <?php endif; ?> <li><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php endif; endwhile; ?> </ul> <?php $a_z_header = 0; endfor; endif; ?>[/php] Or try this: [php]<?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 } } ?>[/php]

Revisions

  • January 22, 2013 @ 02:29:12 [Current Revision] by PeterLugg
  • January 22, 2013 @ 02:25:48 by PeterLugg

Revision Differences

January 22, 2013 @ 02:25:48Current Revision
Content
 Added: I know of no plugin that does this.
 Added: However, some custom coding displaying every letter regardless of having posts or not: <a href="http:// wordpress.pastebin.com/f524c9f4" rel="nofollow" >http://wordpress.pastebin.com/ f524c9f4</a>
 Added: [php]&lt;?php
 Added: // Reference: http://wordpress.org/ support/topic/148727
 Added: $posts = new WP_Query(&quot; cat=$cat&amp; orderby=title&amp; order=ASC&amp; showposts=-1&quot;);
 Added: if ($posts-&gt;have_posts()) :
 Added: for($i='A';$i!='AA';$i++) :
 Added: ?&gt;
 Added: &lt;h3 id=&quot;&lt;?php echo $i; ?&gt;&quot;&gt; &lt;/a&gt;&lt;?php echo $i; ?&gt;&lt;/h3&gt;
 Added: &lt;ul&gt;
 Added: &lt;?php
 Added: while ($posts-&gt;have_posts()) :
 Added: $posts-&gt;the_post();
 Added: if( $i == strtoupper(trim( substr($post- &gt;post_title, 0, 1))) ) :
 Added: ?&gt;
 Added: &lt;li&gt;&lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot; rel=&quot;bookmark&quot; title=&quot;&lt;?php the_title_attribute(); ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;
 Added: &lt;?php
 Added: endif;
 Added: endwhile;
 Added: ?&gt;
 Added: &lt;/ul&gt;
 Added: &lt;?php
 Added: endfor;
 Added: endif;
 Added: ?&gt;[/php]
 Added: Version that only displays letter heading when one or more posts fall under it: <a href="http:// wordpress.pastebin.com/f2302b7cb" rel="nofollow" >http://wordpress.pastebin.com/ f2302b7cb</a>
 Added: [php]&lt;?php
 Added: // Reference: http://wordpress.org/ support/topic/148727
 Added: $posts = new WP_Query(&quot; cat=$cat&amp; orderby=title&amp; order=ASC&amp; showposts=-1&quot;);
 Added: if ($posts-&gt;have_posts()) :
 Added: for($i='A';$i!='AA';$i++) :
 Added: ?&gt;
 Added: &lt;?php
 Added: while ($posts-&gt;have_posts()) :
 Added: $posts-&gt;the_post();
 Added: if( $i == strtoupper(trim( substr($post- &gt;post_title, 0, 1))) ) :
 Added: if( !$a_z_header ) :
 Added: $a_z_header = 1;
 Added: ?&gt;
 Added: &lt;h3 id=&quot;&lt;?php echo $i; ?&gt;&quot;&gt; &lt;/a&gt;&lt;?php echo $i; ?&gt;&lt;/h3&gt;
 Added: &lt;ul&gt;
 Added: &lt;?php
 Added: endif;
 Added: ?&gt;
 Added: &lt;li&gt;&lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot; rel=&quot;bookmark&quot; title=&quot;&lt;?php the_title_attribute(); ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;
 Added: &lt;?php
 Added: endif;
 Added: endwhile;
 Added: ?&gt;
 Added: &lt;/ul&gt;
 Added: &lt;?php
 Added: $a_z_header = 0;
 Added: endfor;
 Added: endif;
 Added: ?&gt;[/php]
 Added: Or try this:
 Added: [php]&lt;?php
 Added: $args = array(
 Added: 'orderby' =&gt; 'title',
 Added: 'order' =&gt; 'ASC',
 Added: 'caller_get_posts' =&gt; 1,
 Added: 'posts_per_page' =&gt; 20,
 Added: );
 Added: query_posts($args);
 Added: if (have_posts()) {
 Added: $curr_letter = '';
 Added: while (have_posts()) {
 Added: the_post();
 Added: $this_letter = strtoupper(substr( $post-&gt;post_ title,0,1));
 Added: if ($this_letter != $curr_letter) {
 Added: echo &quot;&lt;h2&gt; # $this_letter #&lt;/h2&gt;&quot;;
 Added: $curr_letter = $this_letter;
 Added: }
 Added: ?&gt;
 Added: &lt;br /&gt;
 Added: &lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot; rel=&quot;bookmark&quot; title=&quot;Permanent Link to &lt;?php the_title_attribute(); ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;
 Added: &lt;?php }
Deleted: Added: }
 Added: ?&gt;[/php]

Note: Spaces may be added to comparison text to allow better line wrapping.

Tags: , ,

No comments yet.

Leave a Reply