wordpress在循环中的第一个帖子之后包装div中的每3个帖子

时间:2016-07-26 14:44:06

标签: php wordpress loops

我想将我的index.php文件中的每3个帖子添加到行div中,但我想排除第一篇文章,以便我可以对第一篇帖子进行不同的设置。


&#xA ;

我目前每3个帖子都有一个循环工作但是我想排除第一篇文章并对第一篇文章进行不同的设置以使其成为特色帖子





下面是我的代码




 < 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”>' :''; //如果count可被3&#xA整除,则创建开放包装器; $ close =!($ count%3)&& $ count? '< / DIV>' :''; //如果count可被3整除且大于0&#xA,则关闭前一个包装器; echo $ close。$ open;


?>
< div class =“post-main__cell”>
 < a href =“<?php the_permalink()?>” rel =“bookmark”title =“点击进入<?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;否则:?>
  





 <?php echo $ count? '< / DIV>' :''; //如果帖子数大于0,请关闭最后一个包装?>
  





&#xA ;


2 个答案:

答案 0 :(得分:0)

这里有一个很好的答案,可以创建一个循环,将第一个帖子与其余帖子分开:https://wordpress.stackexchange.com/questions/101096/how-to-show-one-post-different-from-the-rest

然后我使用网格框架或一些简单的CSS来设置其余容器的样式,我个人很喜欢getskeleton.com但是这部分取决于你使用博客循环模板。

答案 1 :(得分:0)

我像这样工作

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