Last active
January 21, 2022 03:07
-
-
Save solepixel/5266706 to your computer and use it in GitHub Desktop.
Generate Letter Anchors for your Custom Post Types in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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