【发布时间】:2013-05-23 21:00:24
【问题描述】:
我有以下 PHP 代码,用于输出所有自定义分类值的列表,然后按首字母按字母顺序将它们分组。这工作正常,只是没有输出 URL。有谁能帮忙吗?
<?php
$list = '';
$tags = get_terms( 'film-categories' );
$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 .= '<div class="cat-group"><h3>' . apply_filters( 'the_title', $letter ) . '</h3>';
$list .= '<ul>';
foreach( $tags as $tag ) {
$url = esc_attr( get_tag_link( $tag->term_id ) );
$name = apply_filters( 'the_title', $tag->name );
$list .= '<li><a href="' . $url . '">' . $name . '</a></li>';
}
$list .= '</ul></div>';
}
}
} else $list .= '<p>Sorry, but no tags were found</p>';
echo $list;
?>
【问题讨论】:
-
尝试
echo$tag->term_id变量 - 它的输出是什么? -
它只显示15。模板中有上述代码的页面的帖子ID是5,所以我认为不是问题所在。 (ID为15的帖子完全不相关。)
-
请按照我在回答中的建议替换该行 - 告诉我它是否有效。