• Resolved nightstalker101

    (@nightstalker101)


    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

Viewing 15 replies - 1 through 15 (of 34 total)
  • 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.

    Thread Starter nightstalker101

    (@nightstalker101)

    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.

    You’d have to do it ‘inside a loop’.

    Thread Starter nightstalker101

    (@nightstalker101)

    would it also work with tags?

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

    Thread Starter nightstalker101

    (@nightstalker101)

    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?

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

    Thread Starter nightstalker101

    (@nightstalker101)

    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?

    Thread Starter nightstalker101

    (@nightstalker101)

    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.

    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.

    Thread Starter nightstalker101

    (@nightstalker101)

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

    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?

    Thread Starter nightstalker101

    (@nightstalker101)

    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.

    <?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);
        }
      }
    }
    ?>
    Thread Starter nightstalker101

    (@nightstalker101)

    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?

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