• Resolved joshuaschoenaker

    (@joshuaschoenaker)


    Hi Michael and maybe others,

    I saw the post that I responded in was [solved] so I made a new one:

    I’m having problems with the if ( in_category()).

    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.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Howdy.

    The if (in_category()) { ?> needs to be INSIDE your loop (after the <?php while (have_posts()) : the_post(); ?>).

    See The Loop

    Thread Starter joshuaschoenaker

    (@joshuaschoenaker)

    Thanks for the quick response. But it still outputs ‘Not Found’.

    Code becomes this now right?:

    <?php if (rewind_posts()) : ?>
    
    		<?php while (have_posts()) : the_post(); ?>
    
    		<?php /* If this is in a category archive */ if (in_category()) { ?>
    
    	<ul class="post">
    
    		<?php } ?>
    
    		<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; ?>

    You can check it out here.

    Thread Starter joshuaschoenaker

    (@joshuaschoenaker)

    I’ve also tried this: <?php if (is_single() && in_category()) { ?> but it doesn’t work neither.. I really don’t know what’s wrong here.

    Thread Starter joshuaschoenaker

    (@joshuaschoenaker)

    I’m sorry to keep bothering you, but I truly can’t figure it out. Looked at all the documentation and examples, started all over. Copy pasted the coding from the examples, but it’s still not working.

    Why can’t the single post see in which category it is??

    Switch to the WordPress Default theme. Then in the single.php file, put this code

    <?php
    if ( in_category('1') ) {
    echo 'this post is in category 1';
    } else {
    echo 'this post is NOT in category 1';
    }
    ?>

    after this:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    Change the ‘1’ to whatever category id you have, then visit one of the posts (click on the post title).

    Thread Starter joshuaschoenaker

    (@joshuaschoenaker)

    Yeah, that works. Awesome. But in my own template I have 2 loops. Maybe I did something wrong there?

    Well you’ve got this if (in_category()) vs. this where it is testing a specific category: `if ( in_category(‘1’) )

    So what category are you wanting to test for?

    Thread Starter joshuaschoenaker

    (@joshuaschoenaker)

    I want to test the current category of the post.

    In other words: I want the same list of thumbnails that are on the category page, to appear on the single post which is in that category.

    So when viewing a post in ‘identity design’ (like this one), I want the thumbnails of that category to appear exactly the same as on the category page, (this one).

    Then you need to use Template_Tags/get_the_category.

    Thread Starter joshuaschoenaker

    (@joshuaschoenaker)

    Great man! Thank you very very much!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘in_category(() not working’ is closed to new replies.