【发布时间】:2012-07-18 21:15:30
【问题描述】:
在 wordpress (3.4) 中,我创建了一些代码,它返回自定义标签的字母列表/索引,并在网格布局中过滤自定义帖子。标签被命名为“tagdirectory”。自定义帖子被命名为“目录”。
这是代码:
<?php $list = '';
$tags = get_terms( 'tagdirectory' );
echo '<ul id="portfolio-filter">';
echo'<li><a href="#all" title="">All</a></li>';
$groups = array();
if( $tags && is_array( $tags ) ) {
foreach( $tags as $tag ) {
$first_letter = strtoupper( $tag->name[0] );
$groups[ $first_letter ][] = $tag;}
if( !empty( $groups ) ) {
foreach( $groups as $letter => $tags ) {
$list .= "\n\t" . '<h2>' . apply_filters( 'the_title', $letter ) .'</h2>';
$list .= "\n\t" . '<ul>';
foreach( $tags as $tag ) {
$lower = strtolower($tag->name);
$name = str_replace(' ', ' ', $tag->name);
$naam = str_replace(' ', '-', $lower);
$list .= "\n\t\t" . '<li><a href="#'.$naam.'">'.$name.'</a></li>';
}}}}else $list .= "\n\t" . '<p>Sorry, but no tags were found</p>';print $list;
echo "</ul>";
?>
这很好用,但我也希望显示字母表中的空字母。
例如,现在它返回:
一个
Aicher Otl
阿佩洛格·菲利普
B
低音扫罗
F
Fitszimmons 莫琳
...等等
但它不显示空字母组,因为没有以该字母开头的标签。我确实需要它来显示空组的大写字母,如下所示:
一个
Aicher Otl
阿佩洛格·菲利普
B
低音扫罗
C
D
E
F
Fitszimmons 莫琳
G
...等等
谁能帮助我并告诉我应该添加什么代码才能使其正常工作?
谢谢!
【问题讨论】:
标签: wordpress list tags indexing alphabetical