Support » Fixing WordPress » Show Related Categories in Cat. view

  • Resolved nightstalker101



    I am looking for a plugin or snippet of code, that shows me in the category view related categories.
    Means: When I am in the category x and have posts, that appear in cat. x and y, that cat. y also shall appear on top of cat. x.

    Does anybody have a suggestion, how I can solve this?


Viewing 15 replies - 16 through 30 (of 34 total)
  • Just got bigger 😉

    //cycle through posts and collect all category ids and tags ids used on these posts
    $current_cat = get_query_var('cat');
    $current_tag = get_query_var('tag');
    $all_cats = array();
    $all_tags = array();
    if (have_posts()) :
      while (have_posts()) : the_post();
        $cats = get_the_category();
        $tags = get_the_tags();
        if ($cats) {
          foreach($cats as $cat) {
            if ($cat->term_id != $current_cat) {
              $all_cats[$cat->term_id] = $cat->term_id;
            }  //if ($cat->cat_ID
          } //foreach
        } // if ($cats)
        if ($tags) {
          foreach($tags as $tag) {
            if ($tag->term_id != $current_tag) {
              $all_tags[$tag->term_id] = $tag->term_id;
            }  //if ($tag->term_id
          } //foreach
        } // if ($tags)
    // now display all cats and tags we collected
    $types[0] = 'category';
    $types[1] = 'post_tag';
    foreach ($types as $type) {
      if ($type == 'category') {
        if ($all_cats) {
          $term_ids = $all_cats; //implode(',', $all_cats);
      } else {
        if ($all_tags) {
          $term_ids = $all_tags; //implode(',', $all_tags);
      $taxonomy = $type;
      $args = 'include='.implode(',', $term_ids);
      $terms = get_terms( $taxonomy, $args );
      if ($terms) {
        foreach($terms as $term) {
          if ($term->count > 0) {
            echo '<p>' . '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> has ' . $term->count . ' post(s). </p> ';

    wow and thanks again. You saved the last one hour of my weekend ;).
    One (hopefully) final question:

    echo ‘name ) . ‘” ‘ . ‘>’ . $term->name.’(‘ . $term->count . ‘) | ‘;

    How can I separate between links and tags here?

    Means: One echo only for the Links and one only for the tags? Aaaand 😉 :
    You can see, that I use the dash as separator. How can I remove it from the last item?

    One method is to assign the output to a string then after you’ve iterated though each tag, then use to strip off the last character then echo the output.


    I did it with count and the compared it with the max numbers of items, in that way, I am also able, to break the foreach.

    One Question:
    Which count is there shown behind the items?
    Is it a general count for them or does it count the matches directly?

    In some cases it is wrong, in some others not.
    I am forwarding the links to a special page, with custom queries, so i can see directly if the count is wrong or not.

    And one more Question:
    To separate the request targets, I am using this function:

    $output = '<a rel="nofollow ' . esc_attr(get_term_link($term, $taxonomy)) . '" href="'.get_bloginfo('url').'/detailsuche?in_cat1='.$current_cat.'&in_cat2='.get_cat_id(''.$term->name.'').'" title="Artikel aus den Kategorien '.$cat_title.' und '.$term->name.'  ">' . $term->name.'</a>(' . $term->count . ')';}
    	  elseif (is_tag) {
    	  $output = '<a rel="nofollow ' . esc_attr(get_term_link($term, $taxonomy)) . '" href="'.get_bloginfo('url').'/detailsuche?cat_id='.get_cat_id(''.$term->name.'').'&tag_name='.$current_tag.'&cat_name='.$cat_title.'" title="Alle Posts in der Kategorie '.$term->name.' mit dem Thema '.$cat_title.'">' . $term->name.'</a>(' . $term->count . ')';}
    	  elseif (is_month()){
    	  $output = '<a rel="nofollow" href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "Alle Posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a>(' . $term->count . ')';}

    But it seems, that it works only for tag and cat pages. Why not in archive pages? I tried both: is_month() and is archive().

    But in but cases I get the output for is_category(). Why that?

    yep. I found the error. What about the count in your script?

    The count is from the WHOLE term (category or post_tag) as returned by Function_Reference/get_terms

    I also found the error for the count eventually:
    It counts only the post per page and not per category.
    So I extended the query:

    Now the results are correct, but only in monthly archives. :(.

    would it be possible, to write ht epost id’s in that array, too?

    and this line:

    $args = ‘include=’.implode(‘,’, $term_ids);

    produces an erro in some cases, though the cats aand tags are listed below.

    I am getting this error:
    implode() [function.implode]: Invalid arguments passed in….

    Put an if around that section of the code

    if ($term_ids) (
      $taxonomy = $type;
      $args = 'include='.implode(',', $term_ids);
      $terms = get_terms( $taxonomy, $args );
      if ($terms) {
        foreach($terms as $term) {
          if ($term->count > 0) {
            echo '<p>' . '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> has ' . $term->count . ' post(s). </p> ';

    okay. thanks. And how can I write the post Id’s in that array with cat id’s or an additional array?

    Not sure what you mean so will direct you to the the template tag, query_posts() and something like the ‘post__in’ argument. Good luck.


    no, I meant the loop above, which cycles through all the posts, to get their cats.

    That one where the array with cat id’s and tag id’s is written.
    Would it be possible, to get the id’s of the posts, that are looped there and write the into the array with the terms?

Viewing 15 replies - 16 through 30 (of 34 total)
  • The topic ‘Show Related Categories in Cat. view’ is closed to new replies.