• Resolved Charles Stricklin

    (@charlesstricklin)


    I’d like to have two columns of excerpts on my template: one displaying excerpts from the last N posts of category X, the other displaying excerpts from the last N posts of everything BUT category X. Here’s all I’ve been able to come up with thus far:

    <?php $my_query = new WP_Query('category_name=podcast&showposts=3');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;?>
    <div class="post" id="post-<?php the_ID(); ?>">
    <div class="entry">
    <?php the_excerpt(); ?>
    </div>
    </div>
    <?php endwhile; ?>
    <?php if (have_posts()) : while (have_posts()) : the_post();
    if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
    <div class="post" id="post-<?php the_ID(); ?>">
    <div class="entry">
    <?php the_excerpt(); ?>
    </div>
    </div>
    <?php endwhile; endif; ?>

    Clearly, that’s not going to work, but handling multiple loops is above my head at the moment. Help?!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Really, what you want to do is essentially similar to “Asides” when they’re implemented in a sidebar.

    So, perhaps have a link at the multiple loops stuff at The_Loop and then have a look at Adding_Asides, especially section 3 (sidebar asides).

    Thread Starter Charles Stricklin

    (@charlesstricklin)

    Maybe I’m wrong, but I don’t think that’s going to work in my particular case.

    Thread Starter Charles Stricklin

    (@charlesstricklin)

    Here’s where I stand now:

    <?php $my_query = new WP_Query('category_name=podcast&showposts=3');
    while ($my_query->have_posts()) : $my_query->the_post();?>
    <div class="post" id="post-<?php the_ID(); ?>">
    <div class="entry">
    <?php the_excerpt(); ?>
    </div>
    </div>
    <?php endwhile; ?>
    <?php $my_query = new WP_Query('showposts=3');
    while ($my_query->have_posts()) : $my_query->the_post();?>
    <?php if (!in_category("Podcast")) : ?>
    <div class="post" id="post-<?php the_ID(); ?>">
    <div class="entry">
    <?php the_excerpt(); ?>
    </div>
    </div>
    <?php endif; ?>
    <?php endwhile; ?>

    …but the 2nd loop is returning the 3 most recent posts, regardless of category.

    I am definitely not a coder… but I remember the PHP gurus saying something that having the same query twice, like
    $my_query = new WP_Query
    will not work as expected. If I recall it correctly, you need a “new object” – whatever it is (again, I am code-illiterate).

    Thread Starter Charles Stricklin

    (@charlesstricklin)

    Here’s the final function, thanks to Matt Read (AKA BigJibby)

    <?php
    function wpcommunity_index_loop() {
    global $wp_query; ?>

    <ul>
    <?php $wp_query->query('cat=-4&showposts=5');
    while (have_posts()) : the_post(); ?>
    <li class="nonpodcasts"><?php the_excerpt(); ?></li>
    <?php endwhile;
    $wp_query->query('cat=4&showposts=5');
    while (have_posts()) : the_post(); ?>
    <li class="podcasts"><?php the_excerpt(); ?></li>
    <?php endwhile; ?>
    </ul>
    <?php }
    ?>

    You should realize with this last code that there could be double displayed posts if you have multiple categorys in one post, like 1 and 4. See the adding asides link above for a better way.

    Thread Starter Charles Stricklin

    (@charlesstricklin)

    …but I don’t have multiple categories per post. ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Two columns of posts: one of a certain category, one not?’ is closed to new replies.