WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Show Related Categories in Cat. view (35 posts)

  1. nightstalker101
    Member
    Posted 4 years ago #

    Hi,

    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?

    Thanks,
    Lars

  2. MichaelH
    Member
    Posted 4 years ago #

    In your category template, in The Loop, use Function_Reference/get_the_category and store all the category ids used in all the posts displayed, then use the stored category ids variable with Function_Reference/get_categories, or wp_list_categories(), to display those other categories.

  3. nightstalker101
    Member
    Posted 4 years ago #

    Many Thanks.

    I try to get the id's outside the loop with this funtion:
    <?php
    global $post;
    $categories = get_the_category($post->ID);
    var_dump($categories);
    ?>
    But it only shows me the current category ID and not all the catogories, the post that are inside this one, are assigned to.

  4. MichaelH
    Member
    Posted 4 years ago #

    You'd have to do it 'inside a loop'.

  5. nightstalker101
    Member
    Posted 4 years ago #

    would it also work with tags?

  6. MichaelH
    Member
    Posted 4 years ago #

    Could do that with tags also using get_the_tags($post->ID)

  7. nightstalker101
    Member
    Posted 4 years ago #

    okay,
    I tried in that way:
    if (is_tag()):
    while (have_posts()) : the_post();
    $categories = get_the_category($post->ID);
    endwhile;
    var_dump($categories);
    endif;

    But it does not show all categories, some of them, yes, but not all.

    Why?

  8. MichaelH
    Member
    Posted 4 years ago #

    If you want tags have you tried using get_the_tags($post->ID)

  9. nightstalker101
    Member
    Posted 4 years ago #

    It seems, that the function bewlow and this function:

    global $post;
    $categories = get_the_category($post->ID);

    only fetches the categories form one post in the list. From the first one. Is there any way, to show the categories from all post inside a category or an archive?

  10. nightstalker101
    Member
    Posted 4 years ago #

    It is the same with tags. The functions only fetch the tags or categories only from the first post post in an archive or category, but I need a function, that fetches these data for all the posts in the archive or cat, I am currently in.

  11. MichaelH
    Member
    Posted 4 years ago #

    You'll need to get the tags for each post in that archive, assign it to an array of tags, then after you've looped through all the posts, use get_terms to display all those tags.

  12. nightstalker101
    Member
    Posted 4 years ago #

    hmmmm...could you show me an example, please?
    And:
    get_the_category($post->ID);
    is there any way, to add multiple categories, something like this:
    get_the_category('1,2,3');

  13. MichaelH
    Member
    Posted 4 years ago #

    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.

    Maybe I need to get a better understanding of this. Are you saying when you display a post in a category X archive, then each post is displayed, under each post you want to display the categories for that post, and if the post is in category y (we already know it is in category x) you want to display the categories for that post, with category y, then category x.

    Or are you saying, when you display all the posts in category x, that you want to display a list of all the categories that all those posts use?

  14. nightstalker101
    Member
    Posted 4 years ago #

    Not really:
    On the top of the Archives, I have an additional Navigation, it shall help the user sorting the posts:
    You can sort by tag, category or month. An Example:
    I am browsing the archive of category 'apples', a page of this archive shows in my case seven posts. If some or all of those posts are assigned to another category besides apples, lets say 'peaches' that category shall appear on top, as navigation point.

    Okay: We browse the cat. of apples, see the list on top which shows additional catgeories, the posts in apples are assigned to, you click pears and come to apage, that shows all posts that are assigned to apples and pears.

    The same with the tags.

    Currently, I am trying to call all the categories of all posts in a catgory (with tags it should be the same) with this function:

    global $post;
    $categories = get_the_category($post->ID);
    $catid = get_cat_id(''.single_cat_title("", false).'');
    #var_dump($categories);

    But now I realized, that thsi function shows only the categories for the first post in archive, but not all.

    I hope my description helped you, to understand, what I try to get.

  15. MichaelH
    Member
    Posted 4 years ago #

    <?php
    //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)
      endwhile;
    endif;
    rewind_posts();
    
    // 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 = implode(',', $all_cats);
          wp_list_categories('include='.$term_ids.'&title_li=Categories');
        }
      } else {
        if ($all_tags) {
          $term_ids = implode(',', $all_tags);
          wp_tag_cloud('include='.$term_ids);
        }
      }
    }
    ?>
  16. nightstalker101
    Member
    Posted 4 years ago #

    wow. Many, many thanks, exactly waht I needed.
    I alreade found some parts, but now all is complete..okay, I#d need still a counter behind each, but I can built it with a short loop, that fulfils at least one criteria and insert a counter.
    Or do you have better, eventually more perfomarnt idea?

  17. MichaelH
    Member
    Posted 4 years ago #

    Just got bigger ;)

    <?php
    //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)
      endwhile;
    endif;
    rewind_posts();
    
    // 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);
          //wp_list_categories('include='.$term_ids.'&title_li=Categories');
        }
      } else {
        if ($all_tags) {
          $term_ids = $all_tags; //implode(',', $all_tags);
          //wp_tag_cloud('include='.$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> ';
          }
        }
      }
     }
    ?>
  18. nightstalker101
    Member
    Posted 4 years ago #

    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?

  19. MichaelH
    Member
    Posted 4 years ago #

    One method is to assign the output to a string then after you've iterated though each tag, then use http://php.net/manual/en/function.substr.php to strip off the last character then echo the output.

  20. nightstalker101
    Member
    Posted 4 years ago #

    Hi,

    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.

  21. nightstalker101
    Member
    Posted 4 years ago #

    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?

  22. MichaelH
    Member
    Posted 4 years ago #

  23. nightstalker101
    Member
    Posted 4 years ago #

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

  24. MichaelH
    Member
    Posted 4 years ago #

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

  25. nightstalker101
    Member
    Posted 4 years ago #

    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:
    query_posts('posts_per_page=5000');

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

  26. nightstalker101
    Member
    Posted 4 years ago #

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

  27. nightstalker101
    Member
    Posted 4 years ago #

    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....

  28. MichaelH
    Member
    Posted 4 years ago #

    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> ';
          }
        }
      }
    }
  29. nightstalker101
    Member
    Posted 4 years ago #

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

  30. MichaelH
    Member
    Posted 4 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags