• Heya WP gurus, I could sure use some help here.

    This is my first time dealing with WordPress 3 (using 3.0.1) and somehow I’ve managed to completely mangle up my Loop. I’m not PHP guy, and am trying to implement a more complex WordPress setup than what I’ve done before.

    My goal is a Loop where posts from certain categories are formatted differently (not just style, but actual code, so applying different classes won’t work here). These categories would be 5, 6, and 7. I have a category 8 which is the featured posts that always piggybacks along with category 1 (general purpose blog posts). This will come into play here in a minute.

    I have the following set up as my index.php:

    <?php get_header(); ?>
    <?php get_sidebar(); ?>
    
    <!-- Start the Loop -->
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
        <!-- Start Category Filter -->
        <!-- Cat 5 THOUGHTS -->
        <?php if ( in_category('5') ) { ?>
    
            Lorem Ipsum
    
        <!-- Cat 6 LINKS -->
        <?php } elseif ( in_category('6') ) { ?>
    
            Lorem Ipsum
    
        <!-- Cat 7 VIDEO -->
        <?php } elseif ( in_category('7') ) { ?>
    
            Lorem Ipsum
    
        <!-- Normal BLOG Format    -->
        <?php } else { ?>
    
            Lorem Ipsum
    
        <!-- End Category Filter -->
        <?php } ?>
    
    <!-- 404 Catch -->
    <?php endwhile; else: ?>
    
        Lorem Ipsum
    
    <!-- Stop the Loop -->
    <?php endif; ?>
    
    <?php get_footer(); ?>

    My most recent posts are a category 5 followed by all category 1’s. There is a single category 8, but it is several posts back.

    However, when I preview this theme to check my work, the Loop does not display as I’ve intended. Instead of several posts with the category 5 formatted differently, only the single category 8 post is shown and nothing else (but hey, at least is is formatted right!).

    You can see this in effect at http://4xlt.willphillips.org/.

    My chief finding thus far is that the Loop displays only the first post from category 8 (or has the Feature category along with another). It isn’t a multiple category problem, as a post with any two other categories won’t be displayed.

    However, when I set an elseif(in_category(‘Feature’)){} specifically for the Feature category, it seems to ignore it and goes back to the default format of the else{} and still only displays that single post.

    I have a few suspicions:

    1. Have I inadvertently bungled my PHP (I’m a hack at best)?
    2. Is there a WordPress feature/bug I’m missing?
    3. I have three different query_posts set up in the page (one in the header and two in the sidebar) – can those mess up the Loop?
    4. Have I failed to include some code somewhere?

    Can anyone spot what I’ve done wrong here? I really, really need some help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello TheImperial,

    It looks to me like your trying to get to much mileage out of one WP loop. Try creating a WP loop for each category you wish to display, this will also enable you to change your “display code” inside each loop independently with no fear of hosing other category loops.

    For Example:

    < -- 1st Loop Start -- >
    <?php query_posts('category_name=YOUR_CAT_NAME&showposts=10'); ?>
    <?php while (have_posts()) : the_post(); ?>
    
      Display Markup Here
    
    <?php endwhile;?>
    
    < -- 2nd Loop Start -- >
    <?php query_posts('category_name=YOUR_2ND_CAT_NAME&showposts=10'); ?>
    <?php while (have_posts()) : the_post(); ?>
    
      Display Markup Here
    
    <?php endwhile;?>
    
    etc...

    Hope that helps

    Thread Starter WillPhillips

    (@theimperial)

    Hey CFX,

    Thanks for the reply.

    As it turns out, the problem isn’t with my Loop setup at all – rather, it’s with the three query_posts I had in play in other places on the page.

    The Loop is aping the parameters of the most recent query_posts in play.

    So, when the last query (at the bottom of the sidebar) called ‘tag=featured&showposts=1’, the main Loop showed a single post with that tag.

    I removed that query, and then the Loop fell back to the second query_posts (“Recent Posts – middle of the sidebar) which called for ‘cat=1&showposts=4’, it displayed exactly that. Removing the second, the Loop reverted to the first query (“Featured:” at the very top in the header ).

    Remove all the query_posts, and The Loop works fine.

    I was under the impression that query_posts could be used as I had to set up calls to posts outside of the main Loop.

    Why are my query_posts calls overriding The Loop?

    Thread Starter WillPhillips

    (@theimperial)

    esmi, you just made my month.

    How the heck have I missed that?!?!

    It’s a common oversight. 🙂

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘I've Broken My Loop!’ is closed to new replies.