Forums

[resolved] Show tags that are in a category (3 posts)

  1. rainemaida
    Member
    Posted 9 months ago #

    How would I go about showing all the tags that are used in a certain category and just that category?

  2. MichaelH
    moderator
    Posted 9 months ago #

  3. tugbucket
    Member
    Posted 9 months ago #

    there are 2 different functions in here. not perfect. will leave trailing commas and write duplicates if there are any. It's a start.

    <!-- here -->
    <div>
    <ul>
    <?php
    // this will build a list of all categories and then all posts in them and list all tags used in each post
    foreach (get_categories(array('hide_empty'=>true)) as $category)
    {
    $catid = $category->cat_ID;
    global $post;
    $myposts = get_posts('category='.$catid);
    echo '<li>' . $category->cat_name . "\n";
    echo '<ul>' . "\n";
    foreach($myposts as $post) {
    echo '<li>';
    foreach(get_the_tags($post->ID) as $tag) {
    	echo $tag->name . ', ';
    	}
    
    echo '</li>' . "\n";
    }
    echo '</ul>' . "\n";
    echo '</li>' . "\n";
    }
    ?>
    </ul>
    
    <ul>
    <?php
    // a specific category
    global $post;
    $myposts = get_posts('category=wordpress');
    echo '<li>' . $category->cat_name . "\n";
    echo '<ul>' . "\n";
    foreach($myposts as $post) {
    echo '<li>';
    foreach(get_the_tags($post->ID) as $tag) {
    	echo $tag->name . ', ';
    	}
    echo '</li>' . "\n";
    }
    echo '</ul>' . "\n";
    echo '</li>' . "\n";
    ?>
    </ul>
    </div>
    <!-- here -->

Reply

You must log in to post.

About this Topic