• There seems to be plenty on how to create a loop that excludes a category, and I am putting that to work, but on another part of my theme I need a loop that will only disply posts from one particular category.

    If possible, I’d like it to be able to be a multi-part type thing so I can do things like display the entire contents of the first post in that category, and only the excerpts in the following posts.

    I’ve found a way to do this when I am displaying posts of all categories, and when I am excluding only one category, but for some reason, it won’t work when I try to exclude all but the desired category, etc.

    I’m fairly new at this, so please show everything I’d need. Something like

    query_posts('cat=4');

    would not be enough because I don’t know what else to put around it.

    So far, I’ve used the methods described at perishable press dot com.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter selfcomposed

    (@selfcomposed)

    Thanks but that has little to do with what I’m asking for. I’m needing a loop, the member in the thread you linked to was looking for a list of titles in the sidebar.

    Then look at The Loop article…

    Resources:
    get_posts()
    query_posts()
    Template Hierarchy

    Thread Starter selfcomposed

    (@selfcomposed)

    Michael as far as I can tell, The Loop article (which I had already consulted before posting here) does not tell me how to do what I asked in this thread. If I’m mistaken, please direct me to the example in the article where it does.

    I need a loop that will only disply posts from one particular category.

    If possible, I’d like it to be able to be a multi-part type thing so I can do things like display the entire contents of the first post in that category, and only the excerpts in the following posts.

    Maybe it would help if I showed the code I’m already using to accomplish most of what I want, which is the entire contents of a post from a particular category in one column, possibly followed by excerpts from the next few posts in that category. In the other column, there are excerpts from all the posts excluding the one category presented in the first column.

    So far, I have the second column behaving exactly as I need it to do – there is a loop of all my posts except the ones in a particular category.

    What I can’t figure out is making the part of the loop in the left column only display posts from the particular category.

    // First part of loop in left column
    // This is the only part that doesn't work the way I want
    // This is the part I want to display posts from cat 39 only
    <?php query_posts('showposts=1'); ?>
    <?php $posts = get_posts('numberposts=1&offset=0'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count1 = 0; if ($count1 == "1" ) { break; } else { ?>
    
    // Second part of loop in middle column
    <?php query_posts('showposts=2'); ?>
    <?php $posts = get_posts('numberposts=2&offset=0'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count2 = 0; if ($count2 == "2") { break; } else { ?>
    	<?php if (in_category('39') && !is_single() ) continue; ?>
    
    // Third part of loop
    <?php query_posts('showposts=2'); ?>
    <?php $posts = get_posts('numberposts=2&offset=2'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count3 = 0; if ($count3 == "2") { break; } else { ?>
    	<?php if (in_category('39') && !is_single() ) continue; ?>
    
    // Fourth part of loop
    <?php query_posts('showposts=2'); ?>
    <?php $posts = get_posts('numberposts=2&offset=4'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count4 = 0; if ($count4 == "2") { break; } else { ?>
    		<?php if (in_category('39') && !is_single() ) continue; ?>
    
    // Fifth part of loop
    <?php query_posts('showposts=2'); ?>
    <?php $posts = get_posts('numberposts=2&offset=6'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count5 = 0; if ($count5 == "2") { break; } else { ?>
    		<?php if (in_category('39') && !is_single() ) continue; ?>
    
    // Sixth part of loop - for titles/links only
    <?php query_posts('showposts=8'); ?>
    <?php $posts = get_posts('numberposts=8&offset=8'); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count6 = 0; if ($count6 == "8") { break; } else { ?>
    		<?php if (in_category('39') && !is_single() ) continue; ?>

    For anyone wondering, the reason for breaking it up into sections is I want other content between each part.

    Again, all but the first part of the loop functions the way I want it to.

    I tried using the same exclude stuff – one for each of the other categories but it still showed all the other posts. I don’t understand why that didn’t work either.

    Thread Starter selfcomposed

    (@selfcomposed)

    I have party figured out the solution but its still not quite there.

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php static $count = 1; if ($count == "3") { break; } else { ?>
    
     <?php if (in_category('3') && !is_single() ) continue; ?>
     <?php if (in_category('4') && !is_single() ) continue; ?>
     <?php if (in_category('9') && !is_single() ) continue; ?>
    
    <?php $count++; } ?>
    <?php endwhile; ?>
    <?php endif; ?>

    This functions the way I want in that it only allows posts from the one category, but it falls short of a desireable solution because I have to take the posts out of any other categories for them to show here.

    Is there a way to change the code above so that instead of excluding posts that are in any other category, it shows only the posts in the one particular category regardless what other categories they may belong in?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How do I create a loop that only displays posts from one category?’ is closed to new replies.