WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] is_category() and in_category() not working (10 posts)

  1. nmhall
    Member
    Posted 4 years ago #

    Hello,

    Having some problems with what seems like a simple conditional statement.

    On single.php pages, I'm trying to call in different versions of the loop based on what category I'm in.

    I've tried this:

    `<?php
    if (is_category(7) ) {
    include (TEMPLATEPATH . '/wp-loop2.php');
    } else {
    include (TEMPLATEPATH . 'wp-loop.php');
    }
    ?>`

    Which kicks back a page with no results.

    I've also tried this:

    `<?php
    if (is_category(7) ) {
    include 'wp-loop2.php';
    } else {
    include 'wp-loop.php';
    }
    ?>`

    Which works, but always returns wp-loop.php, regardless of whether or not I'm in category 7.

    Finally, I've also tried this, which is the new and correct way to do what I'm trying to do, according to the wordpress codex:

    `<?php
    if (in_category(7) ) {
    include 'wp-loop2.php';
    } else {
    include 'wp-loop.php';
    }
    ?>`

    Oddly, this bit of code just breaks my page. The wrap of the page is still there, but the content well is empty.

    I've also tried substituting the category number with the category slug, all with similar results.

    Any help is much appreciated.

  2. MichaelH
    Member
    Posted 4 years ago #

    is_category() tests to see if you are displaying a category archive, but you are displaying a single post, so this will always return false

    in_category() tests to see if a given post has a category in that specific category and must be used in The Loop, but you are trying to figure out what the category is, before you get to the loop.

    So might use:

    <?php
    if ( is_single() ) {
    $cats = wp_get_post_categories($posts[0]->ID);
    $first_cat = '';
    if ($cats) {
    $first_cat = $cats[0];
    }
    if ($first_cat == 7 ) {
    include 'wp-loop2.php';
    } else {
    include 'wp-loop.php';
    }
    }
    ?>
  3. nmhall
    Member
    Posted 4 years ago #

    Thanks for the response Michael, I tried it out but still got an empty content well.

    I copied and pasted exactly what you had on the post above, so I might not have adjusted something that needed adjusting (sorry, I'm still learning php).

    Here's the code for the whole single.php code as it sits right now:

    <?php get_header(); ?>

    <div id="content">

    <?php

    if ( is_single() ) {
    $cats = wp_get_post_categories($posts[0]->ID);
    $first_cat = '';
    if ($cats) {
    $first_cat = $cats[0];
    }
    if ($first_cat == 7 ) {
    include 'wp-loop2.php';
    } else {
    include 'wp-loop.php';
    }
    }

    ?>
    <?php comments_template(); ?>

    <div class="navigation">
    <div class="alignleft"><?php next_post_link('%link', '« %title'); ?></div>
    <div class="alignright"><?php previous_post_link('%link', '%title »'); ?></div>
    </div>
    </div>

    <div id="sidebar">
    <?php
    get_sidebar();
    ?>
    </div>

    <?php get_footer(); ?>

    Am I missing something?

  4. nmhall
    Member
    Posted 4 years ago #

    One small update:

    When I insert include (TEMPLATE . '/wp-loop.php'); like so:

    <?php
    if ( is_single() ) {
    $cats = wp_get_post_categories($posts[0]->ID);
    $first_cat = '';
    if ($cats) {
    $first_cat = $cats[0];
    }
    if ($first_cat == 7 ) {
    include (TEMPLATE . '/wp-loop2.php');
    } else {
    include (TEMPLATE . '/wp-loop.php');
    }
    }
    ?>

    I get my comments template to pop back in, as well as the sidebar, though posts are still not showing up.

  5. MichaelH
    Member
    Posted 4 years ago #

    What's in your include files?

    If you change this

    include (TEMPLATE . '/wp-loop2.php');

    to

    echo 'this is a post in category 7';

    do you see the echoed this is a post in category 7?

  6. nmhall
    Member
    Posted 4 years ago #

    When I echo 'this is cat 7' I get 'this is cat 7'.

    After commenting out wp-loop2.php, I get it to work correctly... there must be something in my wp-loop2 that's messing things up (which is strange, since I had it working yesterday).

    Thanks for helping me pinpoint the problem!

  7. annabinna
    Member
    Posted 4 years ago #

    I'm in the same trouble, but I want to modify the archive.php instead of single.php.

    I used this code at the top of the file:

    <?php
    global $wp_query;
    
    if is_category('enunaparaula-cat') {
    	get_header('enunaparaula');
    } else {
        get_header();
    } ?>

    I have the header-enunaparaula.php too and nothing happens! Somebody knows why not?

  8. MichaelH
    Member
    Posted 4 years ago #

    Assuming you are accessing a category archive...

    wouldn't it be
    if ( is_category('enunaparaula-cat') ) {

    If that doesn't resolve it,put an echo 'should be enunaparaula'; after it is_category statement and if you don't see that echoed, then figure out the category id for enunaparaula-cat and use is_category('7') where 7 is the category id.

  9. joshuaschoenaker
    Member
    Posted 4 years ago #

    Hi Michael and maybe others,

    I'm having problems with the if ( in_category()) as well.

    In my archive.php the loop works perfect with the if ( is_category()), but I want to display this list of posts also on the single.php. This means I need to make a multiple loop with the <?php if (rewind_posts()) : ?> function, and this seems to work. But it outputs the 'Not Found' message. It can't seem to figure out which category the single post is in.

    Here is my code:

    <?php if (rewind_posts()) : ?>
    <?php /* If this is in a category archive */ if (in_category()) { ?>
    
    	<ul class="post">
    
    		<?php } ?>
    
    		<?php while (have_posts()) : the_post(); ?>
    
    		<li id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img width="45px" height="40px" src="<?php echo get_post_meta($post->ID, "Thumbnail", true);?>" />
    </a>		</li>
    
    		<?php endwhile; ?>
    						</ul>
    
    	<?php else : ?>
    						<h2 class="center">Not Found</h2>
    		<?php include (TEMPLATEPATH . '/searchform.php'); ?>
    
    	<?php endif; ?>

    I have tried to put '4' and 'identity-design' for example, also without the ( ' ) but no luck..

    This is the code in archive.php that does work:

    <?php if (have_posts()) : ?>
    
    <?php /* If this is a category archive */ if (is_category()) { ?>
    	<ul class="post">
    
    		<?php } ?>
    
    		<?php while (have_posts()) : the_post(); ?>
    							<li id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img width="70px" height="70px" src="<?php echo get_post_meta($post->ID, "Thumbnail", true);?>" />
    </a>
    																		</li>
    
    		<?php endwhile; ?>
    						</ul>
    
    	<?php else : ?>
    						<h2 class="center">Not Found</h2>
    		<?php include (TEMPLATEPATH . '/searchform.php'); ?>
    
    	<?php endif; ?>

    Thank you very much in advance. Would mean a lot.

  10. rumlab
    Member
    Posted 3 years ago #

    Reviving an old thread here.

    The code posted by Michael works except I would like to only specify a top level category only, as the user of the site are free to make their own categories. The code only works when you specify the child category id.

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic