WordPress.org

Ready to get started?Download WordPress

Forums

Same loop, different formatting depending on category? (5 posts)

  1. greasypigstudios
    Member
    Posted 4 years ago #

    I have one loop that shows posts from different categories. I would like to be able to display posts from one category in one way (say, with a thumbnail), and another category another way (i.e., just the excerpt).

    Help?

  2. @mercime
    Volunteer Moderator
    Posted 4 years ago #

  3. greasypigstudios
    Member
    Posted 4 years ago #

    Thanks for the help, but nothing I saw there referred me to what I should do if I wanted to show posts from differing categories all on the ONE SAME loop (like the main index), but the formatting would be different depending on what category the post is.

  4. greasypigstudios
    Member
    Posted 4 years ago #

    I've gotten as far as thinking it'll be a conditional tag inside the loop, but I'm having a hard time getting it to determine whether a specific post is from one category or another:

    <?php
    query_posts('&showposts=10');
    
    global $more;
    // set $more to 0 in order to only get the first part of the post
    $more = 0; 
    
    // the Loop
    while (have_posts()) : the_post();
    ?>
    
    		<?php if(is_category(12)) { ?>
    			<li <?php post_class() ?>>
                		<div class="title">
    
            		<h2 id="post-<?php the_ID(); ?>">BLAH<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    
            		<h3>Posted in: <span><?php the_category(', ') ?></span></h3>
    
            		</div> 
    
    			<!-- .post --></li>
    
    		<?php } else { ?>
    
    	         	<div class="title">
    
                            <h2 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    
                            <h3>Posted in: <span><?php the_category(', ') ?></span></h3>
    
                            </div> 
    
    		<?php } ?>

    Right now I'd want it so that in the loop, if the category is 12, then print the word "BLAH" ahead of the title (just as a test). Currently it's not working. I have a feeling it's because is_category checks to see if the entire PAGE is from a category, not just that specific post.

    Any guidance is much appreciated.

  5. greasypigstudios
    Member
    Posted 4 years ago #

    Ha, figured it out. It's not

    is_category

    it should be

    in_category

    Fantastic. Good to have worked this out loud, and hopefully will help others in future.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.