如何显示三个不同类别的最新帖子?

时间:2012-06-29 作者:Tony

我正试图以这种方式设置我的网站主页。目前,主页上唯一的帖子包含一篇带有播客的文章。简单地说,播客有两个人讨论体育,他们想为每个播客写评论。我为每个人创建了一个单独的类别,我想在播客下面显示他们的评论,如下所示。

播客,

Steve的评论,

马特的评论

我对循环没有太多的研究,正如我所说,我只有最新的播客文章,但现在我需要添加两条评论,每个类别1条。谁能帮帮我,我真的很困惑?我已发布现有循环

<?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(); ?>

1 个回复
SO网友:Stephen Harris

不使用query_posts - 这真的会把事情搞砸。相反,使用三个WP_Query 实例:

$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 :)

结束

相关推荐

使用GET_TEMPLATE_PART发布格式Single-loop.php

我已设置为使用标准之外的post格式库和视频。我正在编辑循环单。php为每个帖子格式提供不同的布局,但我要为每个帖子格式包含get\\u template\\u部分。这就是我所拥有的:<?php /** * The loop that displays a single post. * * The loop displays the posts and the post content. See * http://codex.wordpress.or