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' => '',
);