[英]wordpress wrap every 3 posts in a div after the very first post in a loop
I want to add every 3 posts in my index.php file in a row div but I would like to exclude the first post so that I can style the first post differently. 我想在div行中的index.php文件中添加每3个帖子,但我想排除第一个帖子,以便可以对第一个帖子进行不同的样式设置。
I currently have the loop working for every 3 posts but I would like to exclude the first post and style the first post differently to make it a featured post 我目前每3个帖子都有一个循环,但我想排除第一个帖子,并对第一个帖子进行不同的样式设置,使其成为特色帖子
below is my code 下面是我的代码
<div class="content post-main__content clearfix" id="post-<?php the_ID(); ?>" >
<?php
if(have_posts()) : for($count=0;have_posts();$count++) : the_post();
$open = !($count%3) ? '<div class="post-main__row clearfix">' : ''; //Create open wrapper if count is divisible by 3
$close = !($count%3) && $count ? '</div>' : ''; //Close the previous wrapper if count is divisible by 3 and greater than 0
echo $close.$open;
?>
<div class="post-main__cell">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Click to go to <?php the_title(); ?>">
<div class="post-main__cell-top">
<?php the_post_thumbnail(array(330, 167,)); ?>
<h3 class="content__category-name <?php foreach((get_the_category()) as $category) { echo $category->cat_name. ''; } ?>">
<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?>
</h3>
</div>
<div class="post-main__cell-bottom">
<h2 class="archive-header"><?php the_title() ?></h2>
<p class="paragraph--time"><?php posted_on(); ?></p>
</div>
</a>
</div>
<?php endfor; else : ?>
<?php echo $count ? '</div>' : ''; //Close the last wrapper if post count is greater than 0 ?>
There is a great answer here for creating a loop that separates out the first post from the rest: https://wordpress.stackexchange.com/questions/101096/how-to-show-one-post-different-from-the-rest 这里有一个很好的答案,可以创建一个将第一篇文章与其他文章分开的循环: https : //wordpress.stackexchange.com/questions/101096/how-to-show-one-post-different-from-the-休息
Then I'd use a grid framework or some simple css to style the remaining containers, personally I love getskeleton.com but this part is up to you using the blog loop template. 然后,我将使用网格框架或一些简单的CSS来设置其余容器的样式,就我个人而言,我喜欢getskeleton.com,但这部分取决于您使用博客循环模板。
I worked it like this 我这样工作
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 12, 'paged' => $paged );
query_posts($args); ?>
<?php if (have_posts()) : ?>
<?php $postcount = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $postcount++; ?>
<?php if ($postcount == 1 && $paged == 1) : // if this is the first post & first page ?>
<div class="post-main__row clearfix">
<div class="post-main__cell post-main__cell-large">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Click to go to <?php the_title(); ?>">
<div class="post-main__cell-left clearfix">
<?php the_post_thumbnail(array(691, 317,)); ?>
<h3 class="content__category-name <?php foreach((get_the_category()) as $category) { echo $category->cat_name. ''; } ?>">
<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?>
</h3>
</div>
<div class="post-main__cell-right">
<h2 class="archive-header"><?php the_title() ?></h2>
<p class="paragraph--time"><?php the_time('l, F jS, Y') ?></p>
</div>
</a>
</div>
</div>
<?php
if(have_posts()) : for($count=0;have_posts();$count++) : the_post();
$open = !($count%3) ? '<div class="post-main__row clearfix">' : ''; //Create open wrapper if count is divisible by 3
$close = !($count%3) && $count ? '</div>' : ''; //Close the previous wrapper if count is divisible by 3 and greater than 0
echo $close.$open;
?>
<div class="post-main__cell">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Click to go to <?php the_title(); ?>">
<div class="post-main__cell-top">
<?php the_post_thumbnail(array(330, 167,)); ?>
<h3 class="content__category-name <?php foreach((get_the_category()) as $category) { echo $category->cat_name. ''; } ?>">
<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?>
</h3>
</div>
<div class="post-main__cell-bottom">
<h2 class="archive-header"><?php the_title() ?></h2>
<p class="paragraph--time"><?php the_time('l, F jS, Y') ?></p>
</div>
</a>
</div>
<?php endfor; else : ?>
<?php endif; ?>
<?php echo $count ? '</div>' : ''; //Close the last wrapper if post count is greater than 0 ?>
<?php endif; ?>
<?php endwhile; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.