Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I use this code to get tags from category.

get all tags from category

I want to display in the category page the specific tags. I have 8 categories and this is my code:

        <?php if (is_category('10')) { ?>
    <?php $args = array(
  'categories'        => '10'
);
$tags = get_category_tags($args);
$content .= "<ul>";
foreach ($tags as $tag) {
  $content .= "<li><a href=\"$tag->tag_link\">$tag->tag_name</a></li>";
}
$content .= "</ul>";
echo $content; ?> 

    <?php } elseif (is_category('4')) { ?>
<?php $args = array(
  'categories'        => '4'
);
$tags = get_category_tags($args);
$content .= "<ul>";
foreach ($tags as $tag) {
  $content .= "<li><a href=\"$tag->tag_link\">$tag->tag_name</a></li>";
}
$content .= "</ul>";
echo $content; ?> 
.....

Do that for each category I thinks is not the best way, any idea?

share|improve this question
    
Btw: See update and check for ! empty() before printing the result. –  kaiser Dec 7 '12 at 9:30
add comment

1 Answer 1

up vote 2 down vote accepted

If you use get_terms(), then you can retrieve all terms for a given taxonomy (this includes category as well as post-tag).

To get the category on a category archive page, you can use

get_category( get_query_var( 'cat' ) )

which will give you an object of the currently displayed cat archive page.

So the actual term list will be available with something like the following:

$terms = get_terms(
     get_category( get_query_var( 'cat' ) )
    ,array(
         'fields'       => 'ids'
        ,'hierarchical' => true
        ,'hide_empty'   => false
        ,'pad_counts'   => true
     )
);

$term_links = array();
foreach ( $terms as $term )
{
    $link = get_term_link( $term, $taxonomy );

    ! is_wp_error( $link ) AND $term_links[] = sprintf(
             '<a href="%s" rel="tag">%s</a>'
            ,esc_url( $link )
            ,$term->name
    );
}
// Now do something clever with $term_links
// For example:
! is_empty( $terms ) AND printf(
     '<ul>%s</ul>'
    ,implode( "", $term_links )
);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.