简体   繁体   English

WordPress多循环-查询单个最新帖子-每个帖子的单独类别

[英]Wordpress Multiple Loops - Query Single Most Recent Post - Separate Class for Each Post

My goal here is to use multiple wordpress loops to style each post in a given category separately. 我的目标是使用多个wordpress循环分别为给定类别中的每个帖子设置样式。 I do believe I have it mostly figured out except for the actual query.. 我确实相信,除了实际的查询外,我基本上已经弄清楚了。

I need to be able to query the {MOST RECENT} post in a category in the first loop, then on the second loop query the 2nd most recent post in a category, then the 3rd most recent post in the next loop, enabling me to have separate classes & styles for each. 我需要能够在第一个循环中查询某个类别中的{MOST RECENT}帖子,然后在第二个循环中查询该类别中第二个最新帖子,然后在下一个循环中查询第三个最新帖子,使我能够每种都有单独的类和样式。

Any help would be amazing +++!! 任何帮助都将是惊人的+++!

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

I notice that although you want to use different loops to define unique classes, your loop blocks are largely the same. 我注意到,尽管您想使用不同的循环来定义唯一的类,但是循环块在很大程度上是相同的。 If all you want to do is change the classes of your elements, there's no need to use three separate loops, as that will clutter up your template and end up being much slower than using a single loop. 如果您要做的只是更改元素的类,则无需使用三个单独的循环,因为这会使模板杂乱无章,并且比使用单个循环慢得多。

You should also avoid using query_posts, as it overrides the default Wordpress Loop, and may have unintended consequences, especially if you forget to reset the query. 您还应该避免使用query_posts,因为它会覆盖默认的Wordpress Loop,并且可能会产生意想不到的后果,尤其是在您忘记重置查询的情况下。

The order of the posts in the loop defaults to the most recent posts, so you don't need to worry about setting the ordering parameters. 循环中帖子的顺序默认为最新的帖子,因此您无需担心设置排序参数。

Using your example, I've reworked everything to apply dynamic classes to your wrappers depending on how many iterations the loop has gone through. 使用您的示例,我对所有工作进行了重新设计,以根据循环经历的迭代次数将动态类应用于包装器。 Keep in mind, you can use attributes of the post itself to define your classes to make them unique (in this case, the post ID is used): 请记住,您可以使用帖子本身的属性来定义您的类,以使其具有唯一性(在这种情况下,将使用帖子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
?>

UPDATE: Now your top wrapper will store 4 posts per row. 更新:现在,您的顶层包装器将每行存储4个帖子。 This can be adjusted however you need through the $postsPerRow variable, and you can always increase the Posts_per_page parameter as needed. 可以对此进行调整,但是您需要通过$ postsPerRow变量进行调整,并且始终可以根据需要增加Posts_per_page参数。

EDIT 2: Using WP Query has the added benefit of separating out values you might need. 编辑2:使用WP查询具有分离您可能需要的值的附加好处。 Review the code for an update to your latest question. 查看代码以获取最新问题的更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM