• I’m trying to use in_category() in the footer so that I only show html code when the page is in a particular category. I’m pretty sure that it’s not working because I’m outside the loop.

    If this is a simple matter of pasting a short bit of code (e.g. global $post …), I’d really appreciate it if you could tell me where it is that I’ll need to post the code along with any changes I need to make to the code below which occurs in the footer.php file.

    <?php if(in_category('597') ){ ?> 
    
    A special message for the reader      
    
    <?php } ?>

Viewing 15 replies - 1 through 15 (of 15 total)
  • you want to use “is_category”, you could use “elseif” or “else” to add more variables, or close your “if” with an “endif”.

    Here is the article about conditional tags in the Codex, lots of helpful info there.

    Thread Starter ryancaldwell

    (@ryancaldwell)

    So you’re telling me that “is_category” works when you are on a single post?

    Thread Starter ryancaldwell

    (@ryancaldwell)

    Actually, jleuze did not answer my question (poorly stated as the question was). is_category is only relevant if dealing with a category page.

    I need to be able to check whether the current post is in a category, but I need to be able to do so from within the footer.

    My apologies Ryan, I misunderstood you, I thought you were referring to a category archive page.

    It looks like the problem you are having is because “in_category” is normally used within The Loop. To use it outside of The Loop, it must be coded a little differently.

    Take a look at this page in the Codex about using in_category outside of The Loop.

    Thread Starter ryancaldwell

    (@ryancaldwell)

    jleuze, i had already taken a look at that page in the codex and i can’t figure out how to apply it to my situation.

    For everyone else to test and figure out:
    He and I are running WP 2.7.

    We assign a global variable in the header.php
    We give that variable a value in the single.php
    We try to read that value in footer.php

    By the time we get to Footer.php the variable doesn’t have a value any longer. Why is that?

    Have you identified when the variable disappears? (ie: echo it out from header.php, then echo from single.php, then footer.php). Is the variable name already in use?

    Just a couple of thoughts. It’s hard to tell without seeing the code.

    The example they use in the Codex is a little bit misleading, since it is dealing with single.php, not footer.php.

    But I just tried it and it worked for me with the default Kubrick theme.

    I made three copies of the footer.php file. I called them footer.php, footer-test.php, and footer-normal.php. Both footer-test.php, and footer-normal.php have the original code that was in footer.php, but I added the text “Test Category” to my footer links.

    I created a category called “Test”, and added a few posts to it. In my case, the id # of this category is “3”.

    In the original footer.php file, I deleted everything and added this code:

    <?php
     if ( have_posts() ) { the_post(); rewind_posts(); }
     if ( in_category(3) ) {
     include(TEMPLATEPATH . '/footer-test.php');
     } else {
     include(TEMPLATEPATH . '/footer-normal.php');
     }
     ?>

    This means that if a page contains posts from the “Test” category, footer-test.php is loaded, otherwise. footer-normal.php is loaded.

    This worked for me, your mileage may vary depending on your theme.

    ody3307 – Variable name is custom (nothing is using it) and It does work in single.php as I can assign and echo it out.

    We shouldn’t have to create a loop in the footer to do this, but Ryan already told me that his attempt to do so didn’t work.

    phoenixfireball

    “We assign a global variable in the header.php”

    How are you setting this variable? (session?, etc)
    Are you calling it from within a function?

    global $mycrazyvariable;

    This syntax will only work from within a function.

    Ed N.

    (@enailor)

    ody3307 says:

    This syntax will only work from within a function.

    Yet, I set the same variables and can use ANYWHERE but the footer. In addition, standard WP functions (such as is_front_page() ) are not available in the footer.

    Anyone got a better idea?

    I haven’t found a good workaround for this, either. As of v2.7 in_category() works outside the loop, but doesn’t seem to work in the footer.php file.

    When viewing single.php, I am trying to determine what category the article belongs to, then I am trying to highlight the footer nav accordingly. It doesn’t seem like it should be this hard :*-(

    <?php $page = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1; query_posts(“author=1&showposts=11&paged=$page”); while ( have_posts() ) : the_post() ?>

    I like to show on an author page only posts from one category (sport, #5). What would be the syntax for the if-else statement?

    <?php
    if ( in_category( ‘sport’ )) {
    in_category(5)
    } elseif ( in_category()) {
    ???
    }
    ?>

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘in_category used in footer’ is closed to new replies.