如果您不想硬编码字母表链接,另一种选择是(即,如果没有帖子以字母开头,那么该字母根本不会显示在分页中)。
// Always show pagination
// First, grab all posts.
$posts = get_posts(array(
\'numberposts\' => -1,
\'post_type\' => \'homeless\',
\'orderby\' => \'title\',
\'order\' => \'ASC\'
));
// Next, grab the first letter of each title.
$firstLetters = array();
foreach($posts as $post) {
$title = $post->post_title;
$startingLetter = substr($title, 0, 1);
$dupeFirstLetters[] = $startingLetter;
// Remove duplicates
$firstLetters = array_unique($dupeFirstLetters);
// Alphabetize
sort($firstLetters);
}
foreach($firstLetters as $letter) {
// Output the letter pagination, only for letters that have posts
echo "<a href=\\"?letter=$letter\\">$letter</a>";
}
// If there is a request for a specific "letter" in the query string
if(!empty($_GET[\'letter\'])) {
$letter = $_GET[\'letter\'];
}
// Else, default to showing the first found letter
// i.e. A if there are any post titles starting with A
else {
$letter = $firstLetters[0];
}
// Finally, run a custom query to display the posts - see Max\'s link #1 above for specifics
然后,您可以运行查询,只提取以该字母开头的帖子,并根据需要显示它们。