我的目标是使用多个wordpress循环来分别设置给定类别中每个帖子的样式。我相信除了实际查询之外,我已经弄清楚了大部分。.
我需要能够在第一个循环中查询类别中的{MOST RELATE}帖子,然后在第二个循环中查询类别中的第二个最新帖子,然后在下一个循环中查询第三个最新帖子,使我能够为每个类别和样式设置单独的类和样式。
任何帮助都会很棒+++!
<?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; ?>
我注意到,虽然你想使用不同的循环来定义唯一的类,但你的循环块基本上是相同的。如果您只想更改元素的类,则无需使用三个单独的循环,因为这会使模板混乱,最终比使用单个循环慢得多。
您还应该避免使用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查询具有分离出您可能需要的值的额外好处。查看代码以获取最新问题的更新。