Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display list of tags from current post category WordPress

  • SOLVED

Hi,

I am creating a catalog theme. Each product listed have a single tag (the brand of the product)

example;

Product Name - High Tops
Category - Sneakers
Tag - Sketchers

On single.php view for 'High Tops' I would like to display all the tags associated with the Sneakers category

Answers (2)

2011-12-15

Fahad Murtaza answers:

<?php
query_posts('category_name=Sneakers');
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); //REMOVES DUPLICATES
echo '<pre>'.print_r($tags_arr, true).'</pre>'; //OUTPUT FINAL TAGS FROM CATEGORY

?>


Hope you can get it to work for your needs. Instead of printing array in the last line, you can simply print in the way you like.


xhanubis comments:

Hey Dude,

Thanks for the quick response. Two things,

1. I need the code to spit out a list <ul><li></li></ul>
2. This site has multiple categories so the category cannot be hard coded into single.php - The code need figure out the current top level category and list the tags associated with this category.


Fahad Murtaza comments:

Or you can use


<ul>
<?php
// Please modify the line below. Replace cat=1 with the php variable holding the category ID
// posts_per_page=-1 checks for all posts so it should be intact

query_posts('cat=1&posts_per_page=-1');

// If you have category name instead
// Use the following line of code instead of the one above

//query_posts('category_name=Sneakers');
// Replace 'Sneakers' with the php variable holding the value for category name


if(have_posts()): while (have_posts()) : the_post();
$all_tag_objects = get_the_tags();
if($all_tag_objects){
foreach($all_tag_objects as $tag) {
if($tag->count > 0) {$all_tag_ids[] = $tag -> term_id;}
}
}
endwhile;endif;
$tag_ids_unique = array_unique($all_tag_ids);


// Now use the following code for displaying the tags list with each tag having its own link.

foreach($tag_ids_unique as $tag_id) {
$post_tag = get_term( $tag_id, 'post_tag' );
echo '<li><a href="'.get_tag_link($tag_id).'">'.$post_tag->name.' ( '.$post_tag->count.' )</a></li>';
}?>
</ul>


Hope this helps.


xhanubis comments:

This solves the 2nd issue, however the first issue remains:

instead of query_posts('cat=1&posts_per_page=-1'); I need to have the code check for the current category and then output the tags.


Fahad Murtaza comments:

OK, here it is with the automatic category ID

<ul>

<?php

$category = get_the_category();
if($category[0]){
$cat_id=$category[0]->term_id;
}
query_posts('cat='.$cat_id.'&posts_per_page=-1');



if(have_posts()): while (have_posts()) : the_post();

$all_tag_objects = get_the_tags();

if($all_tag_objects){

foreach($all_tag_objects as $tag) {

if($tag->count > 0) {$all_tag_ids[] = $tag -> term_id;}

}

}

endwhile;endif;

$tag_ids_unique = array_unique($all_tag_ids);





// Now use the following code for displaying the tags list with each tag having its own link.



foreach($tag_ids_unique as $tag_id) {

$post_tag = get_term( $tag_id, 'post_tag' );

echo '<li><a href="'.get_tag_link($tag_id).'">'.$post_tag->name.' ( '.$post_tag->count.' )</a></li>';

}?>

</ul>


xhanubis comments:

That seems to be it but its behaving a little quirky, its running the loop on the page twice...this code is being placed in the sidebar.


Fahad Murtaza comments:

OK

So if outside the loop, I think this should work in your sidebar



<?php
global $post;
$category = get_the_category($post->ID);

if ($category[0]) {

$cat_id = $category[0]->term_id;

}

query_posts('cat=' . $cat_id . '&posts_per_page=-1');

if (have_posts()):
while (have_posts()):
the_post();
$all_tag_objects = get_the_tags();
if ($all_tag_objects) {
foreach ($all_tag_objects as $tag) {
if ($tag->count > 0) {
$all_tag_ids[] = $tag->term_id;
}
}
}
endwhile;
endif;
$tag_ids_unique = array_unique($all_tag_ids);
// Now use the following code for displaying the tags list with each tag having its own link.
foreach ($tag_ids_unique as $tag_id) {
$post_tag = get_term($tag_id, 'post_tag');
echo '<li><a href="' . get_tag_link($tag_id) . '">' . $post_tag->name . ' ( ' .
$post_tag->count . ' )</a></li>';
}
?>
</ul>


xhanubis comments:

Your code is working, except its not giving the tags at the top level.

Imagine a structure like:

shoes
sneakers - Hightops

Your code is giving the tags for sneakers category, I need it to give the tags associated with the top level category shoes which would include sneakers.


Fahad Murtaza comments:

The sidebar code

<?php
global $post;
$category = get_the_category($post->ID);

if ($category[0]) {

$cat_id = category_top_parent_id($category[0]->term_id);

}

query_posts('cat=' . $cat_id . '&posts_per_page=-1');

if (have_posts()):
while (have_posts()):
the_post();
$all_tag_objects = get_the_tags();
if ($all_tag_objects) {
foreach ($all_tag_objects as $tag) {
if ($tag->count > 0) {
$all_tag_ids[] = $tag->term_id;
}
}
}
endwhile;
endif;
$tag_ids_unique = array_unique($all_tag_ids);
// Now use the following code for displaying the tags list with each tag having its own link.
foreach ($tag_ids_unique as $tag_id) {
$post_tag = get_term($tag_id, 'post_tag');
echo '<li><a href="' . get_tag_link($tag_id) . '">' . $post_tag->name . ' ( ' .
$post_tag->count . ' )</a></li>';
}
?>
</ul>


Paste the follwing into the functions.php

<code>
<?php
/**
* Returns ID of top-level parent category, or current category if you are viewing a top-level
*
* @param string $catid Category ID to be checked
* @return string $catParent ID of top-level parent category
*/

function category_top_parent_id ($catid) {

while ($catid) {
$cat = get_category($catid); // get the object for the catid
$catid = $cat->category_parent; // assign parent ID (if exists) to $catid
// the while loop will continue whilst there is a $catid
// when there is no longer a parent $catid will be NULL so we can assign our $catParent
$catParent = $cat->cat_ID;
}

return $catParent;
}

?><code>


xhanubis comments:

Thank you that did it.

Much thanks to you Fahd and to you too as well Robin, your help was greatly appreciated.

2011-12-15

Arnav Joy answers:

Try Following code in your single.php

<?php
query_posts('category_name=Sneakers');
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); //REMOVES DUPLICATES
echo '<pre>'.print_r($tags_arr, true).'</pre>'; //OUTPUT FINAL TAGS FROM CATEGORY

?>


You can make it more further if you want to get tags for more then one categories,


write following code on your functions.php


function get_category_tags($args) {
global $wpdb;
$tags = $wpdb->get_results
("
SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name, null as tag_link
FROM
wp_posts as p1
LEFT JOIN wp_term_relationships as r1 ON p1.ID = r1.object_ID
LEFT JOIN wp_term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
LEFT JOIN wp_terms as terms1 ON t1.term_id = terms1.term_id,

wp_posts as p2
LEFT JOIN wp_term_relationships as r2 ON p2.ID = r2.object_ID
LEFT JOIN wp_term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
LEFT JOIN wp_terms as terms2 ON t2.term_id = terms2.term_id
WHERE
t1.taxonomy = 'category' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND
t2.taxonomy = 'post_tag' AND p2.post_status = 'publish'
AND p1.ID = p2.ID
ORDER by tag_name
");
$count = 0;
foreach ($tags as $tag) {
$tags[$count]->tag_link = get_tag_link($tag->tag_id);
$count++;
}
return $tags;
}


in your single.php

$args = array(
'categories' => '12,13,14' //pass the id's of the categories here
);
$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;


xhanubis comments:

See response to Fahd, pls.


Arnav Joy comments:

use this code in your single.php to get name of the parent category of the post



<?php
$currentPostID = get_the_ID();
$category = get_the_category($currentPostID);
$mainCatID = $category[0]->term_id;
$parentcatid = get_top_parent_category($mainCatID);
$parentCatName = get_cat_name($parentcatid);
?>

write this code in your functions.php


function get_top_parent_category($cat_ID)
{
$cat = get_category( $cat_ID );
$new_cat_id = $cat->category_parent;

if($new_cat_id != 0)
{
return (get_top_parent_category($new_cat_id));
}
return $cat_ID;
}


Now in single.php use this name of the category in previous function

<?php
query_posts('category_name='.$parentCatName);
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);
//echo '<pre>'.print_r($tags_arr, true).'</pre>';
?>

<ul>
<?php
for($i=0;$i<count($tags_arr);$i++){
?>
<li><?php echo $tags_arr[$i]; ?></li>
<?php } ?>
</ul>


xhanubis comments:

Ok my bad,

I didn't explain as clearly as I should, the code I am looking is to display the tags for the current single post category in the sidebar.


Arnav Joy comments:

ok no problem , just use all the codes of single.php in sidebar.php


sidebar.php

<?php
wp_reset_query();
$currentPostID = get_the_ID();
$category = get_the_category($currentPostID);
$mainCatID = $category[0]->term_id;
$parentcatid = get_top_parent_category($mainCatID);
$parentCatName = get_cat_name($parentcatid);
?>

write this code in your functions.php


function get_top_parent_category($cat_ID)
{
$cat = get_category( $cat_ID );
$new_cat_id = $cat->category_parent;

if($new_cat_id != 0)
{
return (get_top_parent_category($new_cat_id));
}
return $cat_ID;
}


Now in sidebar.php use this name of the category in previous function

<?php
query_posts('category_name='.$parentCatName);
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
for($i=0;$i<count($tags_arr);$i++){
?>
<li><?php echo $tags_arr[$i]; ?></li>
<?php } ?>
</ul>


xhanubis comments:

should both those blocks go into sidebar.php?


Arnav Joy comments:

yes ,

here is all the code for the sidebar:-

<?php
wp_reset_query();
$currentPostID = get_the_ID();
$category = get_the_category($currentPostID);
$mainCatID = $category[0]->term_id;
$parentcatid = get_top_parent_category($mainCatID);
$parentCatName = get_cat_name($parentcatid);

query_posts('category_name='.$parentCatName);
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
for($i=0;$i<count($tags_arr);$i++){
?>
<li><?php echo $tags_arr[$i]; ?></li>
<?php } ?>
</ul>


code for functions.php

function get_top_parent_category($cat_ID)
{
$cat = get_category( $cat_ID );
$new_cat_id = $cat->category_parent;

if($new_cat_id != 0)
{
return (get_top_parent_category($new_cat_id));
}
return $cat_ID;
}


xhanubis comments:

Hey Robin,

That code doesn't work...it actually somehow causes the products to display incorrectly name and content as well as it only displays the tag associated with the current product.


Arnav Joy comments:

i did not get your last point ?
is tag names are outputting correctly , and product name is not coming right.

please explain what is the problem in the output , i am can solve this.


Arnav Joy comments:

use wp_reset_query(); in sidebar.php as follows

endwhile; endif;

wp_reset_query(); // use at this place.

$tags_arr = array_unique($all_tags_arr);