Title: Loop anchor links
Last modified: August 19, 2016

---

# Loop anchor links

 *  Resolved [peterjharrison](https://wordpress.org/support/users/peterjharrison/)
 * (@peterjharrison)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/loop-anchor-links/)
 * Hi All,
 * Not sure if anyone can help me but I’m try to add anchor links with the wordpress
   loop but can’t find anyone who has achieved what I need to do.
 * I have my loop that lists about 30 posts in alphabetical order using query_posts.
   What I want to do is everytime time a new letter begins it adds an anchor link
   for the start of that letter e.g.
 * 
    [Post a1 Content] [Post a2 Content]
 * 
    [Post b1 Content] [Post b2 Content]
 * etc…
 * Is this at all possible and if so can you give me some advice.
 * Regards
    Peter

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/loop-anchor-links/#post-1169949)
 * Can you drop a copy of the code that you’re currently using in that template 
   file into the [WordPress pastebin](http://wordpress.pastebin.ca/)?
 *  [Mike Little](https://wordpress.org/support/users/mikelittle/)
 * (@mikelittle)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/loop-anchor-links/#post-1169969)
 * Try something like this at the top of your loop:
 *     ```
       <?php if (have_posts()) : ?>
           <?php $last_letter = ''; ?>
       		<?php while (have_posts()) : the_post(); ?>
                   <?php $this_letter = substr(get_the_title(), 0,1);
                         if ($this_letter != $last_letter) {
                             echo '<p id="letter-' . $this_letter. '">Anchor for letter ' . $this_letter . '</p>';
                             $last_letter = $this_letter;
                         } ?>
       ... rest of loop
       ```
   
 *  note: only briefly tested but seems to work.
 * By the way adding (empty) anchor tags is old school, better to add an id to an
   appropriate element like my example.
 * Mike
 *  Thread Starter [peterjharrison](https://wordpress.org/support/users/peterjharrison/)
 * (@peterjharrison)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/loop-anchor-links/#post-1169974)
 * Hi Mike,
 * Thanks for your reply! Not tried it yet but will give it a go when I get in later,
   the logic is really good so I don’t see why it wouldn’t work.
 * Your advice is much appraciated as always!
 * Regards
    Pete

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Loop anchor links’ is closed to new replies.

## Tags

 * [alphabetical](https://wordpress.org/support/topic-tag/alphabetical/)
 * [anchor](https://wordpress.org/support/topic-tag/anchor/)
 * [links](https://wordpress.org/support/topic-tag/links/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 3 participants
 * Last reply from: [peterjharrison](https://wordpress.org/support/users/peterjharrison/)
 * Last activity: [16 years, 8 months ago](https://wordpress.org/support/topic/loop-anchor-links/#post-1169974)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
