logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$30
Get tags for current category

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(); ?>

This question has been answered.

xhanubis | 07/23/12 at 2:15am Edit

Previous versions of this question: 07/24/12 at 3:20am | 07/24/12 at 3:21am

(7) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    07/23/12
    2:20am
    Rashad Aliyev says:

    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";
    }

    ?>

    • 07/23/12 2:22am

      Rashad Aliyev says:

      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; ?>

    • 07/23/12 2:28am

      xhanubis says:

      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.

    • 07/23/12 2:33am

      Rashad Aliyev says:

      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

  • avatar
    Last edited:
    07/23/12
    2:28am
    Arnav Joy says:

    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

    ?>

    • 07/23/12 2:35am

      Arnav Joy says:

      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

      ?>

    • 07/24/12 3:26am

      Arnav Joy says:

      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");

    • 07/24/12 3:33am

      Arnav Joy says:

      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>';
      }

      ?>

    • 07/24/12 4:03am

      xhanubis says:

      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.

  • avatar
    Last edited:
    07/23/12
    2:36am
    Manoj Raj says:

    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);
    ?>

  • avatar
    Last edited:
    07/23/12
    2:56am
    AdamGold says:

    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.

    Previous versions of this answer: 07/23/12 at 2:46am | 07/23/12 at 2:46am | 07/23/12 at 2:54am | 07/23/12 at 2:56am

    • 07/23/12 4:54am

      xhanubis says:

      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>

    • 07/23/12 4:57am

      AdamGold says:

      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, ',');
      }

    • 07/23/12 5:04am

      xhanubis says:

      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.

    • 07/23/12 5:08am

      AdamGold says:

      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>

  • avatar
    Last edited:
    07/23/12
    3:47pm

    This answer was downvoted by the top experts.

  • avatar
    Last edited:
    07/23/12
    8:18pm

    This answer was downvoted by the top experts.

  • avatar
    Last edited:
    07/24/12
    3:54am

    This answer was downvoted by the top experts.

This question has expired.



Gabriel Reguly, Hai Bui, Manoj Raj, xhanubis voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.