【问题标题】:wordpress wrap every 3 posts in a div after the very first post in a loop在循环中的第一个帖子之后,wordpress 将每 3 个帖子包装在一个 div 中
【发布时间】:2016-11-30 06:55:27
【问题描述】:

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

我目前每 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">' : ''; //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 ?>

【问题讨论】:

    标签: php wordpress loops


    【解决方案1】:

    我是这样工作的

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2013-03-03
        • 2014-01-22
        • 2011-12-19
        • 2015-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多