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

Get tags for current category WordPress

  • SOLVED

Ok I am breaking this question in two since so far I have not been able to get a solution.

On the sidebar of the category template I would like to display the tags associated the current selected category a drop down.

The code I am using is below.

the Problem is the code works sometimes, the ID echoed at the bottom is only correct some of the time. Can anyone say why this is so and how do I fix it?

<select onchange="if(this.options[this.selectedIndex].value != ''){window.top.location.href=this.options[this.selectedIndex].value}" class="modal" >
<?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&orderby=title&order=ASC&show_count=0&exclude=7,162,169,163,170,164,165,166,167,168,35,34,33,32,36');

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 '<option value="?tag=' . $post_tag->slug . '">' . $post_tag->name . '</option>';
}

?>
</select>

<?php echo $cat_id; wp_reset_query(); ?>

Answers (7)

2012-07-23

Arnav Joy answers:

in category.php write following
<?php
query_posts('category_name='.single_cat_title());
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

?>


Arnav Joy comments:

in single.php write following
<?php
$post_categories = wp_get_post_categories( get_the_ID() );
$cats = array();

foreach($post_categories as $c){
$cat = get_category( $c );

$cats[] = $cat->term_id;
}


$cats = implode(',',$cats);

query_posts('cat='.$cats);
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

?>


Arnav Joy comments:

try replacing your query_posts with this

query_posts("cat=" . get_query_var('cat'). "&posts_per_page=-1&orderby=title&order=ASC&show_count=0&exclude=7,162,169,163,170,164,165,166,167,168,35,34,33,32,36");


Arnav Joy comments:

what you are using is of single page so when you use this
$category = get_the_category($post->ID);
it will show you the correct result when you are in single page but on the category page it will not give you correct result..
what you can do to test that if you are on single page or in category page

so lets try this

<?php
if(is_single()) {
global $post;
$category = get_the_category($post->ID);
if ($category[0]) {
$cat_id = $category[0]->term_id;
}
}

elseif(is_category()) {
$cat_id = get_query_var('cat');
}


query_posts('cat=' . $cat_id . '&posts_per_page=-1&orderby=title&order=ASC&show_count=0&exclude=7,162,169,163,170,164,165,166,167,168,35,34,33,32,36');

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 '<option value="?tag=' . $post_tag->slug . '">' . $post_tag->name . '</option>';
}

?>


xhanubis comments:

Works great on the category page...replacing the query solved the issue. I will vote that you get paid the full amount thank you. The code with the if statement does not work on the single page but I will put that in another question if needs be.

2012-07-23

Rashad Aliyev answers:

Take this code.




<?php

$cat_name = "Test Cat";
query_posts('category_name='.$cat_name); //give the your category name here.

if (have_posts()) : while (have_posts()) : the_post();
$postTags = get_the_tags();
if ($postTags) {
foreach($postTags as $tag) {
$all_tags[] = $tag -> name;
}
}
endwhile; endif;

if(!empty($all_tags)) {
$unique_tags = array_unique($all_tags); //Removes the duplicates tags in the all_tags_arr array.
echo '<pre>'.print_r($unique_tags, true).'</pre>';
} else {
echo "No post tags are available for ".$cat_name." - category";
}

?>


Rashad Aliyev comments:

Get the post tags for current post/page.



<?php
$current_post_tags = wp_get_post_tags($post->ID);
$post_tags_link .= '<ul class="post-tags-link">';
for($i=0; $i < count($current_post_tags ); $i++) {
$post_tags_link .= '<li><a href ="'.get_bloginfo('url').'/'.$current_post_tags[$i]->slug.'">'.$current_post_tags[$i]->name.'</a></li>';
}
$post_tags_link .= '</ul>';
//echo "<pre>";print_r($current_post_tags);
?>
Find more about:<?php echo $post_tags_link; ?>


xhanubis comments:

Hi Rashad,

Thanks for the quick response. the $cat_name variable needs to be dynamic, not hard coded. Also the snippet for the single post sidebar needs to return all the tags of the category that the post belongs to, NOT just the tag of the current post.


Rashad Aliyev comments:

Then you can use :

http://codex.wordpress.org/Function_Reference/get_the_category

or

http://codex.wordpress.org/Function_Reference/get_the_category_by_ID

2012-07-23

Manoj Raj answers:

For dynamic purpose

You can combine the following snippet with any of the answers above (you need to add at the start)

global $wp_query;
$current_category = $wp_query->get_queried_object();
$current_category_name = $current_category->name;

query_posts('category_name='.$current_category_name);




<?php
global $wp_query;
$current_category = $wp_query->get_queried_object();
$current_category_name = $current_category->name;

query_posts('category_name='.$current_category_name);

if (have_posts()) : while (have_posts()) : the_post();
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$all_tags[] = $tag->term_id;
}
}
endwhile;
endif;

$tags_arr = array_unique($all_tags);
$tags_str = implode(",", $tags_arr);

$args = array(
'smallest' => 12,
'largest' => 12,
'unit' => 'px',
'number' => 0,
'format' => 'list',
'include' => $tags_str
);
wp_tag_cloud($args);
?>

2012-07-23

AdamGold answers:

For the tags in the sidebar, I'd recommend a widget, so please add this code to your functions.php (will show tags of current category / category of post):

function cattags_register_widgets() {
register_widget( 'WP_Widget_CatTags' );
}

add_action( 'widgets_init', 'cattags_register_widgets' );


class WP_Widget_CatTags extends WP_Widget {

public $name = 'Category Tags';
public $widget_desc = 'Adds a tag list to your sidebar.';
/* List all controllable options here along with a
default value.
The values can be distinct for each instance of the
widget. */
public $control_options = array();
public $default_data = array();

// The constructor.
function __construct() {
$widget_options = array(
'classname' => __CLASS__,
'description' => $this->widget_desc,
);
parent::__construct( __CLASS__, $this->name,$widget_options,$this->control_options);
}
function form( $instance ){
extract($data);
?>
<p>Widget title:<br />
<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $title; ?>" /></p>
<?php
}
function widget( $args, $instance ) {
extract($data);
if( !isset($title) ) {
$title = '';
}
// show only on category page
if( is_category() ) {
$this->show_tags_list($title, 'cat');
} else if( is_single ) { // show only on post page
$this->show_tags_list($title, 'post');
}
}

function update( $new_instance, $old_instance ) {
$instance = array();
foreach( $new_instance as $key => $value ) {
$instance[ $key ] = $value;
}

return $instance;
}

function show_tags_list($title, $action = '') {
global $wp_query;
echo '<h3>' . $title . '</h3>';

if( $action == 'cat' ) {
$category_name = get_query_var('cat');
} else if( $action == 'post' ) {
$category_post = get_the_category($wp_query->post->ID);
foreach( $category_post as $value ) {
$category_name .= $value->cat_name . ',';
}

$category_name = rtrim($category_name, ',');
}

if( $category_name != '' ) {
query_posts('category_name='.$category_name);

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

$postTags = get_the_tags();

if ($postTags) {
foreach($postTags as $tag) {
$current_cat_tags[] = $tag ->name;
}
}

endwhile; endif;


if(!empty($all_tags)) {
$print_tags = '';
$unique_tags = array_unique($all_tags);

foreach( $unique_tags as $value ) {
$print_tags .= $value . ',';
}

$print_tags = rtrim($print_tags, ',');
echo $print_tags;
} else {

echo "No post tags are available for ".$category_name.".";

}
}

}

}


After pasting it to functions.php and saving, drag the widget (Appearances->Widgets) to your desired sidebar.


xhanubis comments:

Unfortunately none of the above code work.

The closest thing so far is this code...but for some reason it works on one category but not on the others.

<select onchange="if(this.options[this.selectedIndex].value != ''){window.top.location.href=this.options[this.selectedIndex].value}" class="modal" >
<?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&orderby=title&order=ASC&show_count=0');

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;

wp_reset_query();

$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 '<option value="?tag=' . $post_tag->slug . '">' . $post_tag->name . '</option>';
}
?>
</select>


AdamGold comments:

Replace:

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

}


with:

if (! empty($category)) {
$cat_id = '';
foreach( $category as $value ) {
$cat_id .= $value->term_id;
}
$cat_id = rtrim($cat_id, ',');
}


xhanubis comments:

That cause my code to stop returning tags all together. I have set the original code to echo the category from what I see its not updating the category when I change category. I start with category 3 and no matter which category I click it still echo 3.


AdamGold comments:

ah I see, so change:
<select

to:
<form method="POST" action=""><select name="category_select">

Change:
if ($category[0]) {
$cat_id = $category[0]->term_id;
}


to:
if ($category[0]) {
$cat_id = $category[$_POST['category_select']]->term_id;
}


And add after </select>:
<input type="submit" name="submit" value="Change" /></form>

2012-07-23

anhogganwesto answers:

Tai Chi Kleidung

2012-07-23

lretaglemarge answers:

Guangzhou prestige waterproof electronic watches have genuine fa - $33.80 :

2012-07-24

ewienermerl answers:

Die Liebo lange T-Shirt