• I’m trying to display post from two categories on my home page, and to style the categories differently. They are subcategories of “menu-items,” “seasonal-special” and “regular-dish.”

    Seems like a pretty common task. Here’s the only code I can figure out that even comes close to working:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    	 <!-- "seasonal specials" is category 4, so I exclude all the
    others. This is tedius, and every time I add a category I have to
    add it here -->
    	 <?php if (in_category('1')||in_category('3')||in_category('5')
    ||in_category('6')||in_category('7')||in_category('8')
    ||in_category('9')) continue; ?>
    
    <div class="post">
    content goes here
    </div>
    
    	<?php endwhile; ?>
    
    	<!-- This grabs the newest 4 menu items -->
    	<?php query_posts('category_name=regular-dishes&showposts=4');
    	while (have_posts()) : the_post();
    	$do_not_duplicate = $post->ID; ?>
    
    <div class="menu-item-right">
    content goes here
    </div>
    
     	<?php endwhile; else: ?>
    
    <p>Sorry, no posts matched your criteria.</p>
    
    <?php endif; ?>

    This works erratically. For example, currently no articles from cat 4 (seasonal specials) are showing up. Before I added cat 9 (which is a subcategory of menu items, just like cat 4) the cat 4 content was showing up correctly.

    There has to be a better way to do this…

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter exit6

    (@exit6)

    Having a hard time reading that post, with the code spilling out into his sidebar, but he’s not sorting by category but by post date i think.

    You might have to copy and paste the examples, but he’s not sorting by category. You would just need to add cat=4 or whatever to the get_posts arguments. The idea is to show you multiple loops.

    Related:
    query_posts()
    get_posts()

    Thread Starter exit6

    (@exit6)

    Thanks, got it. Yeah I have multiple loops going on, but I must be doing something wrong because they act unpredictably. I’m a decent AS3 programmer and the loops work a little different there, or what’s more likely is I’m not used to php’s syntax.
    An example of erratic behavior, I’ve got multiple loops on a category page. The category has three subcategories. I use this little gem:
    <?php if (in_category('1')||in_category('2')||in_category('3')||in_category('4')||in_category('5')||in_category('6')||in_category('7')||in_category('9')) continue; ?>
    To get to the first sub-category, but for some reason it’s only putting up 8 entries, even though there are 13 in that sub-category. What’s even stranger is at one point it was putting up 9!
    Later I use this code which I like better:

    <?php query_posts('category_name=drinks');
    while (have_posts()) : the_post();
    $do_not_duplicate = $post->ID; ?>

    But if I try to replace that first ugly one with this prettier one, I get errors regarding the end of the loop.

    Thread Starter exit6

    (@exit6)

    Aha!

    I looked at the query_post() link and caught what I was doing wrong with the loop — I needed to stick the query before the loop.

    <?php
    query_posts('category_name=regular-dishes');
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>
    <div class="menu-item-right">...the content...</div>
    
    <?php
    endwhile;
    wp_reset_query();
    ?>

    Now it’s letting 10 of the 13 posts through, which is still weird.

    How do I copy the same subcategories for different categories without retyping each time?

    Please help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Displaying posts from certain categories’ is closed to new replies.