Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tag list made from specific category - wordpress

Is this feature built into wordpress? i didnt see anything within the codex.

codex.wordpress.org/Function_Reference/wp_tag_cloud

I have a few pages that are category specific and i would like to show all the tags associated with those posts.

I did find this, but im not sure if its proper or if a better way exists (source)(old method!!!!):

<?php
    query_posts('category_name=html');
    if (have_posts()) : while (have_posts()) : the_post();
        $posttags = get_the_tags();
        if ($posttags) {
            foreach($posttags as $tag) {
                $all_tags_arr[] = $tag -> name;
            }
        }
    endwhile; endif; 

    $tags_arr = array_unique($all_tags_arr);
?>
    <ul>
<?php
    foreach($tags_arr as $tag){
        echo '<li>'.$tag.'</li>';
    }
?>
</ul>
<?php wp_reset_query(); ?>

UPDATE( simplified ):::

to make a list of tags from a specific category this code is much better(just change the category name):

::Recently updated again because of a loop error::

    <ul>
                <?php
                    query_posts('category_name=html');
                    if (have_posts()) : while (have_posts()) : the_post();

                        if( get_the_tag_list() ){
                            echo $posttags = get_the_tag_list('<li>','</li><li>','</li>');
                        }

                    endwhile; endif; 

                    wp_reset_query(); 
                ?>
</ul>

Even tough i may have a solution, please update this if a new one comes around.

like image 309
chasethesunnn Avatar asked Mar 16 '11 05:03

chasethesunnn


3 Answers

I think the method you've found it's the only way you can achieve what you're looking for. Maybe you can modify some lines, but the concept is right.

at the moment i don't think there's a way to filter tags as you would using a core wordpress function.

like image 62
bluantinoo Avatar answered Oct 23 '22 08:10

bluantinoo


I did not get the code above to work my installation of WordPress. I did however manage to tweak it until it worked. Here is my tweak:

$catid = get_cat_ID(single_cat_title("",false));
$catobj = get_category($catid);
$catslug = $catobj->slug;
$all_tags_arr = array();
query_posts('category_name='.$catslug);
if (have_posts()) : while (have_posts()) : the_post();
    $posttags = get_the_tags();
    if ($posttags) {
        foreach($posttags as $tag) {
            $all_tags_arr[] = $tag -> term_id;
        }
    }
endwhile; endif; 

$tags_arr = array_unique($all_tags_arr);

$tagcloud_args = array(
    'include'   =>  implode(',',$tags_arr),
);

wp_tag_cloud( $tagcloud_args ); 
wp_reset_query();
like image 2
Börkur Sigurbjörnsson Avatar answered Oct 23 '22 08:10

Börkur Sigurbjörnsson


Here is a much easier example.... Just change the category name and hey presto your done. The associated tags will print out in a list format.

<?php query_posts('category_name=html'); if (have_posts()) : while (have_posts()) : the_post();

    $posttags = get_the_tags();

    if ($posttags) {
        foreach($posttags as $tag) {
            $all_tags[] = $tag -> name;
        }
    }
    endwhile; endif; 

    //This snippet removes any duplicates.
    $tags_unique = array_unique($all_tags); 

    echo '<ul>';
        foreach($tags_unique as $unique) {
          echo  '<li>'.$unique.'</li>';
        }
    echo '</ul>';

    wp_reset_query();

?>
like image 2
Michael Durney Avatar answered Oct 23 '22 10:10

Michael Durney