Skip to content

Instantly share code, notes, and snippets.

@solepixel
Last active January 21, 2022 03:07
Show Gist options
  • Save solepixel/5266706 to your computer and use it in GitHub Desktop.
Save solepixel/5266706 to your computer and use it in GitHub Desktop.
Generate Letter Anchors for your Custom Post Types in WordPress
<?php
$alphabet = range('A','Z');
$letters = array();
while( $the_query->have_posts() ) : $the_query->the_post();
$lname = get_post_meta(get_the_ID(), $prefix.'lname', true);
$first = strtoupper(substr(trim($lname), 0, 1));
if($first && !in_array($first, $letters)){
$letters[] = $first;
}
endwhile;
echo '<ul class="letters">';
foreach($alphabet as $letter){
if(in_array($letter, $letters)){
$format = '<li><a href="#%1$s">%1$s</a></li>';
} else {
$format = '<li><span>%1$s</span></li>';
}
echo sprintf($format, $letter);
}
echo '</ul>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment