WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] in_category Working on Part of Page, Not Rest (11 posts)

  1. gbrown88
    Member
    Posted 1 year ago #

    Hello,

    Have a weird situation going on with in_category that I can't seem to figure out, so any help would be great. Here's the situation:

    I currently am using in_category in 5 places on my site where depending on the category, different content is displayed in those areas.

    1 of the spots is in the header before the Loop and the other 4 are in the Sidebar and Footer after the Loop.

    The 1st one, when I place is above my header area, it works fine. When I move it below the header (where I ideally want it), it doesn't work.

    Same code in each area, any thoughts?

    Thanks

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Are you using it inside the loop? If not you have to pass it the post ID:
    http://codex.wordpress.org/Function_Reference/in_category

    // post ID 34
    if(in_category('fruit', 34)){ ... }
  3. gbrown88
    Member
    Posted 1 year ago #

    How can I dynamically pass the post id through?

    I tried adding, $post->ID in the area you place 34 and that didn't work.

    Thanks

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    On what template file are you using this? The post object is available outside the loop in single.php and page.php:
    in_category('fruit', $post->ID)

  5. gbrown88
    Member
    Posted 1 year ago #

    I'm using the Genesis Framework.

    I'm running it on is_single only though, in an area designated 'genesis_before_content_sidebar_wrap'.

    When I have it in 'genesis_before_header' it works fine. Same code.

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    Maybe you need to get the global $post value outside the loop. try this:

    global $post;
    if (in_category('fruit', $post->ID)){...}

    Does the theme query the loop in single.php?

  7. gbrown88
    Member
    Posted 1 year ago #

    I'll try this. What's odd is that the other 4 areas, using the same code are working perfectly.

  8. gbrown88
    Member
    Posted 1 year ago #

    Yeah, that didn't work. This is what I tried:

    if(is_single()) {
    		global $post;
    		if (in_category('fruit', $post->ID)) { ?>
    
                             //Do Stuff
    
    <?php } else { ?>
    
            //Do Stuff
    
       	<?php }
    	}
  9. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    what template?
    does the template use any custom query?
    if so, is the query reset after the loop?

  10. gbrown88
    Member
    Posted 1 year ago #

    That's exactly it! Thanks @alchymyth.

    I have a couple custom quereis in a drop down of the Navigation bar.

    Thanks

  11. keesiemeijer
    moderator
    Posted 1 year ago #

    I'm sorry but I'm not that familiar with the Genesis Framework. did you ask at their forums?

    Try echoing the post ID (for troubleshooting):

    global $post;
    echo 'post ID = ' .  $post->ID;

Topic Closed

This topic has been closed to new replies.

About this Topic