我正在尝试建立我的分类法的字母索引。我正在提取该术语的第一个字母并将其显示在页面上。但是,我只想显示第一个字母,如果它是一个新字母。这样我就可以将所有 a 组合在一起,然后将 b 组合在一起,依此类推。我想我可以使用帖子计数来做到这一点,但它只适用于第一个和第二个帖子。任何其他帖子都会输出第一个字母。任何帮助将不胜感激谢谢!
$post_type = 'book';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type )
);
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective
posts
$terms = get_terms( $taxonomy );
$count = 0;
foreach( $terms as $term ) :
$count++;
$current_letter = '';
if ($count == 1) :
$title_letter1 = strtoupper(substr($term->name,0,1));
if ($title_letter1 != $current_letter) {
echo "<h3>$title_letter1</h3>";
$current_letter = $title_letter1;
}
?>
<?php echo $term->name; ?>
<?php elseif ($count >= 2) :
$title_letter2 = strtoupper(substr($term->name,0,1));
if ($title_letter2 != $title_letter1 and $title_letter2 !=
$current_letter ) {
echo "<h2>$title_letter2 </h2>";
$current_letter = $title_letter2;
}?>
<?php echo $term->name; ?></div>
<?php else : ?>
<?php endif; ?>