Forums

Need in_category() code, please (2 posts)

  1. igendreau
    Member
    Posted 1 year ago #

    Pretty new to WordPress and PHP. Always been an ASP guy, but starting to learn. What I want to do is edit the code below, so that if the post is in the category "Men", it gets the CSS class "men", if it's in the category of "Women", it's in the CSS class "women", and if it's in neither, it stays in the class it's currently in. Can someone help? Thanks!

    <?php get_header(); ?>
    	<div id="content" class="narrowcolumn">
    		<?php if (have_posts()) : ?>
    			<?php while (have_posts()) : the_post(); ?>
    				<div class="post" id="post-<?php the_ID(); ?>">
    					<div class="postwrapper">
    						<a href="<?php the_permalink() ?>" rel="bookmark" title="Continue reading the rest of this entry »"><img src="<?php $key="thumbnail"; echo get_post_meta($post->ID, $key, true); ?>" alt="thumbnail" class="thumbnail" /></a>
    						<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    						<div class="entry">
    							<?php the_excerpt(); ?>
    						</div>
    					</div>
    					<div class="postmetadata">
    						<span class="thetime"><?php the_time('F jS, Y') ?></span>
    						<span class="thecategory"><?php the_category(', ') ?></span>
    						<?php edit_post_link('Edit', '<span class="editthispost">', '</span>'); ?>
    						<span class="thecomments"><?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></span>
    						<div class="readmore">
    							<a href="<?php the_permalink() ?>" rel="bookmark" title="Continue reading the rest of this entry »">Read More</a>
    						</div>
    					</div>
    				</div>
    
    		<?php endwhile; ?>
    
    				<div class="navigation">
    					<div class="navleft"><?php next_posts_link('Older Entries') ?></div>
    					<div class="navright"><?php previous_posts_link('Newer Entries') ?></div>
    				</div>
    
    	<?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    		<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    
    	<?php endif; ?>
    
    	</div>
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    this might work:

    <?php while (have_posts()) : the_post(); ?>
    				<div class="post" id="post-<?php the_ID(); ?>" <?php if( in_category('men') ) echo 'class="men"'; } elseif( in_category('women') ) { echo 'class="women"'; } ?>>

Topic Closed

This topic has been closed to new replies.

About this Topic