Get tags specific category

I use this code to get tags from category.

get all tags from category

Read More

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?

Related posts

Leave a Reply

1 comment

  1. 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 )
    );