How do you set a active tag in wordpress that matc

2019-09-14 02:03发布

问题:

I'm trying to set an active class on a tag list with the below code:

    <?php
        $categories = get_the_category();
        $category_id = $categories[0]->cat_ID;
        $args = array( 'category' => $category_id, 'post_type' =>  'post' ); 
        $postslist = get_posts($args);  ?> 
        <div class="archieve__list">
            <ul class="row" data-equalizer data-equalize-on="medium">
                <?php foreach ($postslist as $post) :  setup_postdata($post); ?>  
                    <?php the_tags( '
                            <li class="column small-12 medium-4 text-center float-left archieve__item"><span class="archieve__link" data-equalizer-watch>', '</span></li>
                    '); ?>
                <?php endforeach; ?> 
            </ul>
        </div>

I've looked into using get_tags() however this pulls back all of the tags where I only want it to pull back the tags of the current category. This code below does what I want, setting an active state on the tag in use but I again need it to just output the tags of that category:

<ul id="blog-tags">
<?php
$tags = get_tags();
if ( $tags ) {
foreach ( $tags as $tag ) {
    echo '<li>';

    if ( (int) $tag->term_id === get_queried_object_id() )
        echo "<b>$tag->name</b>";
    else
        printf(
            '<a href="%1$s">%2$s</a>',
            get_tag_link( $tag->term_id ),
            $tag->name
        );

    echo '</li>';
}
}
?>
</ul>

I really would like the latter option to work as it feels a lot cleaner than the first option.

回答1:

I'm not sure if you actually mean "Tags of the current category" as Categories and Tags are both taxonomies. But if you want to pull the tags of the current post you can use wp_get_post_tags()