admin管理员组文章数量:1321066
I'm displaying a lit of tags when they have a description. I'd like to sort them by name instead of by post quantity. Is there a way to do this?
<?php
$tags = get_tags();
if ($tags) {
foreach ($tags as $tag) {
if ($tag->description) {
echo '<dt><a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . '><h3>' . $tag->name.'</h3></a></dt><dd style="margin-bottom: 10px;">' . $tag->description . '</dd>';
}
}
}
?>
I'm displaying a lit of tags when they have a description. I'd like to sort them by name instead of by post quantity. Is there a way to do this?
<?php
$tags = get_tags();
if ($tags) {
foreach ($tags as $tag) {
if ($tag->description) {
echo '<dt><a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . '><h3>' . $tag->name.'</h3></a></dt><dd style="margin-bottom: 10px;">' . $tag->description . '</dd>';
}
}
}
?>
Share
Improve this question
asked Aug 31, 2011 at 17:41
Zach ShallbetterZach Shallbetter
1,2147 gold badges22 silver badges45 bronze badges
2 Answers
Reset to default 1You should use the function wp_tag_cloud, it has many parameters as described in the codex and the one to sort the results as you want: 'orderby' => 'name', 'order' => 'ASC',
.
Sorry, but the above answer is lacking a lot of flexibility. wp_tag_cloud()
is going to output a whole slew of HTML, and nothing friendly to manipulate in the PHP.
get_tags()
already has support for orderby
in the input array, since it's a term query (reference: the Codex)
So here's a sample of how you'd do that, and I'm listing the full options below
$tags = get_tags(array(
'taxonomy' => 'post_tag',
'orderby' => 'name',
'hide_empty' => false // for development
));
Big ol' list of all available parameters with their defaults:
$args = array(
'taxonomy' => null,
'object_ids' => null,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
'include' => array(),
'exclude' => array(),
'exclude_tree' => array(),
'number' => '',
'offset' => '',
'fields' => 'all',
'count' => false,
'name' => '',
'slug' => '',
'term_taxonomy_id' => '',
'hierarchical' => true,
'search' => '',
'name__like' => '',
'description__like' => '',
'pad_counts' => false,
'get' => '',
'child_of' => 0,
'parent' => '',
'childless' => false,
'cache_domain' => 'core',
'update_term_meta_cache' => true,
'meta_query' => '',
'meta_key' => '',
'meta_value' => '',
'meta_type' => '',
'meta_compare' => '',
);
本文标签: taxonomySorting tags by name
版权声明:本文标题:taxonomy - Sorting tags by name 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742090856a2420259.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论