WordPress.org

Ready to get started?Download WordPress

Forums

in_category($category_object) is true for categories not related to the post (4 posts)

  1. newbieee
    Member
    Posted 1 year ago #

    Hello,

    I've got a menu build from links to all categories. If page is_single() I would like to highlight the categories in menu that belongs to the current post. The problem is, that for some posts the in_category($category) function returns true even for categories it does not belong to.

    I simplified the function just to check where is the problem, but it still returns more categories that the post belongs to.

    foreach (get_categories('exclude=1&parent=0&hide_empty=1&number=6') as $category) {
    if (is_single())
      if (in_category($category))
       echo $category->name." ";
    }

    BUT this works correctly (i.e. returns only name of the category it really belongs)

    foreach (get_categories('exclude=1&parent=0&hide_empty=1&number=6') as $category) {
    if (is_single()) {
              $categories = get_the_category();
              foreach($categories as $cat)
                echo $cat->name." ";
    }
    }

    It's located in header.php Can you see any other possible problem with that? What else should I post to find the culprit?

    Thank you for reply!

  2. luckdragon
    Member
    Posted 1 year ago #

    might be something simple.. like

    if (in_category($category,$_post))

  3. newbieee
    Member
    Posted 1 year ago #

    That's what I also thought, but your solution still keeps me giving two categories.

    I read that some of the functions you can't use inside or outside The Loop so i guess that's the problem (i'm in header.php and i didn't call <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> so i think I'm outside the loop - is that right?).
    http://wordpress.org/support/topic/single_cat_title-not-displaying-anything

    I temporarily solved it with this:

    foreach (get_categories('exclude=1&parent=0&hide_empty=1&number=6') as $category) {
              if (is_single()) {
                $cat_flag = false;
                foreach(get_the_category() as $cat)
                  if ($category->cat_ID == $cat->cat_ID)
                  {
                    $cat_flag = true;
                    break;
                  }
                if ($cat_flag) {
                  //do my code
                }
    }

    But simplier solution would be nicer..

  4. Digital Raindrops
    Member
    Posted 1 year ago #

    (i'm in header.php and i didn't call <?php if ( have_posts() )

    In header php use is_single() or is_singular( $post_type ), this code is not tested!

    // Is it a single post outside the loop?
    if ( is_single() || is_singular( array('post') ) ) {
       // Use the $post global
       global $post;
       // Get the categories from the post meta
       $categories = wp_get_post_categories( $post->ID );
       if ($categories) {
         //Do menu stuff here!
       }
    }

    HTH

    David

Topic Closed

This topic has been closed to new replies.

About this Topic