Support » Themes and Templates » Displaying tags from within category loop.

  • Lucas



    Im currently developing a theme where I within a standard loop in the archive.php (based off the code from the underscores (_s) theme) needs to get the tags, related to the categories, as links.

    I’ve run into a basic problem with PHP, which im not very keen at remembering from school days.
    I want to display each array item as an link.

    The idea is simple I want to display each tag as a link to the tag itself.

    I reused some code from the following thread: Get Tags specific to Category

    Mixed in with some code that adds the category nicename.
    It is run within a standard loop:

    <?php if ( have_posts() ) : ?>

    With a <?php if ( is_category() ) {

    Here is the “final” tag code:

    $category = get_the_category();
                    $cat = $category[0]->category_nicename;
                    	if (have_posts()) : while (have_posts()) : the_post();
                            $posttags = get_the_tags();
                    		if ($posttags) {
                    			foreach($posttags as $tag) {
                    				$all_tags_arr[] = $tag -> name; //Using just $tag making $all_tags_arr a multi-dimensional array, which does work with array_unique
                    	endwhile; endif;
                      $tags_arr = array_unique($all_tags_arr); //Remove duplicates
                      	echo '<pre>'.print_r($tags_arr, true).'</pre>'; //Here I need to output as links instead, possibly through a foreach function before...

    I think I need to go with the foreach function in PHP, but I have no clue in how to achieve this with the given code above.
    If further information is needed please don’t hesitate to ask.

  • The topic ‘Displaying tags from within category loop.’ is closed to new replies.