• Hi. On my index page, I want to have two columns. One displays the most recent posts from the category “articles” and the other displays the most recent posts from the category “news.” I’ve looked at the codex information on multiple loops but I can’t seem to get anything to work. Any ideas? Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)
  • What exactly doesn’t work (based on Codex article)? Just to make sure: did you replace the “Do stuff..” portion with the proper template tags?

    More details required. Works for lots of other people….

    Thread Starter zarastudios

    (@zarastudios)

    This is what I did. I still don’t know how to display the news category first, then the articles category. Does anyone have an example that I could look at?

    <p class="title">News</p>
    <p />
    <?php if (have_posts()) : ?><br />
    <?php while (have_posts()) : the_post(); ?>
    <div class="post">
    <div class="post-info"><h2 class="post-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    </div>
    <div class="post-content">
    <?php the_content(); ?>
    <div class="post-info">
    <p class="post-date">Posted on <?php the_time('D j M Y'); ?> <?php edit_post_link('(edit this)'); ?></p>
    <?php wp_link_pages(); ?>
    </div>
    <!--
    <?php trackback_rdf(); ?>
    -->
    <div class="post-footer">&nbsp;</div>
    </div>
    <?php comments_template(); // Get wp-comments.php template ?>
    </div>
    <?php endwhile; ?>
    <div class="navigation">
    <div class="alignleft"><?php posts_nav_link('','','&laqu Previous Entries') ?></div>
    <div class="alignright"><?php posts_nav_link('','Next Entries ??','') ?></div>
    </div>
    <?php else : ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
    <p class="title">Articles</p>
    <p />
    <?php $my_query = new WP_Query('category_name=articles&showposts=2'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div class="post">
    <div class="post-info"><h2 class="post-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    </div>
    <div class="post-content">
    <?php the_content(); ?>
    <div class="post-info">
    <p class="post-date">Posted on <?php the_time('D j M Y'); ?> <?php edit_post_link('(edit this)'); ?></p>
    <?php wp_link_pages(); ?>
    </div>
    <!--
    <?php trackback_rdf(); ?>
    -->
    <div class="post-footer">&nbsp;</div>
    </div>
    <?php comments_template(); // Get wp-comments.php template ?>
    </div>
    <?php endwhile; ?>

    Thread Starter zarastudios

    (@zarastudios)

    Well, I tried something a little different and got the following error for the News Category, but the Articles Category worked fine:

    WordPress database error: [Not unique table/alias: ‘wp_post2cat’]
    SELECT DISTINCT * FROM wp_posts LEFT JOIN wp_post2cat ON (wp_posts.ID = wp_post2cat.post_id) LEFT JOIN wp_post2cat ON (wp_posts.ID = wp_post2cat.post_id) WHERE 1=1 AND (category_id = 4) AND post_date_gmt <= ‘2005-07-08 03:27:59’ AND (post_status = “publish” OR post_author = 1 AND post_status != ‘draft’ AND post_status != ‘static’) AND (category_id = 1) GROUP BY wp_posts.ID ORDER BY post_title ASC LIMIT 0, 2

    This is what I did:


    <p class="title">News</p>
    <p />
    <?php $top_query = new WP_Query('cat=4&showposts=2'); ?>
    <?php while($top_query->have_posts()) : $top_query->the_post(); ?>
    <div class="post">
    <div class="post-info"><h2 class="post-title"><a href="<?php

    the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php

    the_title(); ?>"><?php the_title(); ?></a></h2>
    </div>
    <div class="post-content">
    <?php the_content(); ?>
    <div class="post-info">
    <p class="post-date">Posted on <?php the_time('D j M Y'); ?> <?php

    edit_post_link('(edit this)'); ?></p>
    <?php wp_link_pages(); ?>
    </div>
    <!--
    <?php trackback_rdf(); ?>
    -->
    <div class="post-footer">&nbsp;</div>
    </div>
    <?php comments_template(); // Get wp-comments.php template ?>
    </div>
    <?php endwhile; ?>
    <p class="title">Articles</p>
    <p />
    <?php query_posts('cat=1&showposts=2'); ?>
    <?php while(have_posts()) : the_post(); ?>
    <div class="post">
    <div class="post-info"><h2 class="post-title"><a href="<?php

    the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php

    the_title(); ?>"><?php the_title(); ?></a></h2>
    </div>
    <div class="post-content">
    <?php the_content(); ?>
    <div class="post-info">
    <p class="post-date">Posted on <?php the_time('D j M Y'); ?> <?php

    edit_post_link('(edit this)'); ?></p>
    <?php wp_link_pages(); ?>
    </div>
    <!--
    <?php trackback_rdf(); ?>
    -->
    <div class="post-footer">&nbsp;</div>
    </div>
    <?php comments_template(); // Get wp-comments.php template ?>
    </div>
    <?php endwhile; ?>

    I also noticed that when I added posts from the News category, they didn’t display on the index page, only posts from the Articles category show up. Thanks in advance for your help.

    If that doesn’t work you may want to take a look at this thread:
    https://www.ads-software.com/support/topic/38046

    Thread Starter zarastudios

    (@zarastudios)

    Thanks!

    Zarastudios:

    It appears that the first part of your code doesn’t have any posts to work with. You need to query the posts twice. Check the examples in the codex under ‘the loop’.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display posts from two categories on index page’ is closed to new replies.