Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to set up the homepage of my site in this manner. Right now, the only post on the homepage contains an article with an podcast. To make it simple, The podcast has two people discussing sports and they want to write their commentary for each podcast. I created a separate category for each person and I'd like to display their commentaries below the podcast as below.

Podcast,

Steve's commentary,

Matt's commentary

I haven't worked much with the Loop and like I said I have only the latest podcast article but now I need to add the two commentaries, 1 from each category. Can anyone help me, i'm really confused ? I've posted my existing loop

<?php 
query_posts('category_name=Weekly Show&showposts=1');
if (have_posts()) : while (have_posts()) : the_post(); ?> 
<div class="art-Post">
<div class="art-Post-body">
<div class="art-Post-inner art-article">
<h2 class="art-PostHeader">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Permanent Link to %s', 'kubrick'), the_title_attribute('echo=0')); ?>">
<?php the_title(); ?>
</a>
</h2>
<?php ob_start(); ?>
<?php $icons = array(); ?>
<?php if (!is_page()): ?><?php ob_start(); ?><img src="<?php bloginfo('template_url'); ?>/images/PostDateIcon.png" width="18" height="18" alt="" />
<?php the_time(__('F jS, Y', 'kubrick')) ?>
<?php $icons[] = ob_get_clean(); ?><?php endif; ?><?php if (!is_page()): ?>    <?php ob_start(); ?><?php _e('Author', 'kubrick'); ?>: <?php the_author_posts_link() ?>
<?php $icons[] = ob_get_clean(); ?><?php endif; ?><?php if (current_user_can('edit_post', $post->ID)): ?><?php ob_start(); ?><?php edit_post_link(__('Edit', 'kubrick'), ''); ?>
<?php $icons[] = ob_get_clean(); ?><?php endif; ?><?php if (0 != count($icons)): ?>
<div class="art-PostHeaderIcons art-metadata-icons">
<?php echo implode(' | ', $icons); ?>

</div>
<?php endif; ?>
<?php $metadataContent = ob_get_clean(); ?>
<?php if (trim($metadataContent) != ''): ?>
<div class="art-PostMetadataHeader">
<?php echo $metadataContent; ?>

</div>
<?php endif; ?>
<div class="art-PostContent">

      <?php if (is_search()) the_excerpt(); else the_content(__('Read the rest of this entry &raquo;', 'kubrick')); ?>
      <?php if (is_page() or is_single()) wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>

</div>
<div class="cleared"></div>
<?php ob_start(); ?>
<?php $icons = array(); ?>
<?php if (!is_page()): ?><?php ob_start(); ?><img src="<?php bloginfo('template_url'); ?>/images/PostCategoryIcon.png" width="18" height="18" alt="" />
<?php printf(__('Posted in %s', 'kubrick'), get_the_category_list(', ')); ?>
<?php $icons[] = ob_get_clean(); ?><?php endif; ?><?php if (!is_page() && get_the_tags()): ?><?php ob_start(); ?><img src="<?php bloginfo('template_url'); ?>/images/PostTagIcon.png" width="18" height="18" alt="" />
<?php the_tags(__('Tags:', 'kubrick') . ' ', ', ', ' '); ?>
<?php $icons[] = ob_get_clean(); ?><?php endif; ?><?php if (!is_page() && !is_single()): ?><?php ob_start(); ?><img src="<?php bloginfo('template_url'); ?>/images/PostCommentsIcon.png" width="18" height="18" alt="" />
<?php comments_popup_link(__('No Comments &#187;', 'kubrick'), __('1 Comment &#187;', 'kubrick'), __('% Comments &#187;', 'kubrick'), '', __('Comments Closed', 'kubrick') ); ?>
<?php $icons[] = ob_get_clean(); ?><?php endif; ?><?php if (0 != count($icons)): ?>
<div class="art-PostFooterIcons art-metadata-icons">
<?php echo implode(' | ', $icons); ?>

</div>
<?php endif; ?>
<?php $metadataContent = ob_get_clean(); ?>
<?php if (trim($metadataContent) != ''): ?>
<div class="art-PostMetadataFooter">
<?php echo $metadataContent; ?>

</div>
<?php endif; ?>

</div>

    <div class="cleared"></div>
</div>
</div>

<?php comments_template(); ?>
<?php endwhile; endif; ?>

</div>
<?php include (TEMPLATEPATH . '/sidebar2.php'); ?>
</div>
<div class="cleared"></div>

<?php get_footer(); ?>
share|improve this question
    
add comment

1 Answer

Don't use query_posts - it's can really mess things up. Instead, use three WP_Query instances:

$post_from_cat_a  = new WP_Query(array( 
    'category__name' => array('a'),  //Get posts from category a
    'posts_per_page'=> 1 //Limit it to the latest one
));
if( $post_from_cat_a->have_posts() ){
    while( $post_from_cat_a->have_posts() ): $post_from_cat_a->the_post();
         //Display output here for post from category a
    endwhile;
}

$post_from_cat_b  = new WP_Query(array( 
    'category__name' => array('b'), 
    'posts_per_page'=> 1
));
if( $post_from_cat_b->have_posts() ){
  ...
  ...
//and so on :)
share|improve this answer
    
Thanks Stephen, I think I understand to use the WP Query but I'm not sure how to fit it in to my existing code. It looks like a lot of the code in my template is the look of how the post is displayed. Do I need to repeat that after each post ? –  Tony Jun 29 '12 at 22:19
    
So normally you'd have one loop that displays your latest posts. You don't want that, so you can remove it. Instead replace if with the three loops from the above three queries (each with only one post). You can format it so it 'looks' like just one loop - but technically it'll be three one after the other. Inside the while loops dictates what's shown (i.e. post title, excerpt, or a template to be used). You can just copy what's in your current main loop, into each of the three loops that you add. –  Stephen Harris Jun 30 '12 at 10:17
    
I'd probably recommend using the Transient API too with 3 additional queries. –  helgatheviking Feb 13 '13 at 1:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.