Wordpress多圈 - 查询单个最新帖子 - 每个帖子的单独类

时间:2012-05-12 14:21:44

标签: php wordpress loops

我的目标是使用多个wordpress循环分别为给定类别中的每个帖子设置样式。我确实认为除了实际的查询之外我总是想出来。

我需要能够在第一个循环中查询某个类别中的{MOST RECENT}帖子,然后在第二个循环中查询某个类别中第2个最新帖子,然后是下一个循环中第3个最新帖子,让我有单独的课程和每种风格。

任何帮助都会令人惊叹+++ !!

<?php if (have_posts()) : ?>
           <?php query_posts('category_name=Main&posts_per_page=1&={MOST RECENT}'); ?>
               <?php while (have_posts()) : the_post(); ?>    

                            <div class="row1">
                                <div class="one">
                                    <div class="post_data">
                                        <div class="icons_right"><img src="pop_out_icon.png" alt="pop out icon" /></div>
                                        <h1 class="post_title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                                        <h2 class="post_snippet">Lorem Ipsum Dolar Sit Amet.</h2>
                                        <p class="post_date"><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></p>
                                    </div> <!-- post_data //-->
                                <?php the_content(); ?>
                            </div>  <!-- 1 //-->
               <?php endwhile; ?>

                 <?php query_posts('category_name=Main&posts_per_page=1&={SECOND MOST RECENT}'); ?>
               <?php while (have_posts()) : the_post(); ?>    

                            <div class="row2">
                                <div class="two">
                                    <div class="post_data">
                                        <div class="icons_right"><img src="pop_out_icon.png" alt="pop out icon" /></div>
                                        <h1 class="post_title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                                        <h2 class="post_snippet">Lorem Ipsum Dolar Sit Amet.</h2>
                                        <p class="post_date"><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></p>
                                    </div> <!-- post_data //-->
                                <?php the_content(); ?>
                            </div>  <!-- 2 //-->
               <?php endwhile; ?>

     <?php endif; ?>

1 个答案:

答案 0 :(得分:1)

我注意到虽然你想使用不同的循环来定义唯一的类,但你的循环块大致相同。如果您只想更改元素的类,则不需要使用三个单独的循环,因为这会使模板变得杂乱,最终比使用单个循环慢得多。

您还应该避免使用query_posts,因为它会覆盖默认的Wordpress循环,并且可能会产生意想不到的后果,尤其是在您忘记重置查询时。

循环中帖子的顺序默认为最新帖子,因此您无需担心设置排序参数。

使用您的示例,我已经重新设计了所有内容,以便根据循环经历的迭代次数将动态类应用于包装器。请记住,您可以使用帖子本身的属性来定义您的类以使它们唯一(在这种情况下,使用帖子ID):

<?php
$count = 0;
$postsPerRow = 4; //<-- This will help set your top wrapper
$query = new WP_Query('category_name=Main&posts_per_page=3');
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
if($count<(floor($query->found_posts/$postsPerRow)*$postsPerRow)){
    $open = !($count%$postsPerRow) ? '<div class="row row-'.(floor($count/$postsPerRow)+1).'">' : '';
    $close = !($count%$postsPerRow) && $count ? '</div>' : '';
    echo $close.$open;
?>    
    <div class="<?php echo "loop-$count post-".get_the_ID(); ?>">
        <div class="post_data">
            <div class="icons_right">
                <img src="pop_out_icon.png" alt="pop out icon" />
            </div>
            <h1 class="post_title">
                <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
            </h1>
            <h2 class="post_snippet">Lorem Ipsum Dolar Sit Amet.</h2>
            <p class="post_date"><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></p>
        </div> <!-- post_data //-->
        <?php the_content(); ?>
    </div>  <!-- 1 //-->
<?php
$count++;
}
endwhile;endif;
echo $count ? '</div>' : ''; //<-- Close row wrapper
?>

更新:现在,您的顶部包装器每行会存储4个帖子。这可以通过$ postsPerRow变量进行调整,并且您可以随时根据需要增加Posts_per_page参数。

编辑2 :使用WP Query可以分离出您可能需要的值。查看代码以了解最新问题的更新。