简体   繁体   English

我需要在div中包装每4个wordpress帖子

[英]I need to wrap every 4 wordpress posts in a div

I need a div to wrap every four posts in the wordpress loop. 我需要一个div将wordpress循环中的每四个帖子包装起来。 So it would be like 所以就像

<div>
four posts
</div>
<div>
four posts
</div>

my current code is this 我当前的代码是这个

<?php 
        $i = 0;
        $wrap_div = "<div class='frak'>";
            if ( have_posts() ) {
               echo $wrap_div;
                while ( have_posts() ) {
                    the_post();

        ?>

        <div class="wine-section">
            <?php the_post_thumbnail(); ?>
            <div class="wine-info">
                <a href="<?php the_permalink(); ?>"><?php the_title( '<h1>', '</h1>' ); ?></a>
                <?php the_meta(); ?>
            </div><!-- /wine-info -->
            <div class="c"></div>
        </div><!-- /wine-section -->

        <?php       
                if ($i % 4 == 0) { echo "</div>" . $wrap_div; }
                } // end while
            } // end if
            $i++;
        ?>

This code wraps every post individually. 此代码分别包装每个帖子。 Any ideas? 有任何想法吗?

As geomagas pointed out - you are incrementing the variable outside of the loop. 正如地理专家指出的那样-您正在循环外增加变量。 Then 0 % 4 == 0 does evaluate to true - that's because when you divide 0 by 4, you get 0. In order to get around this situation, you need one more rule. 那么0 % 4 == 0计算结果为true-这是因为当您将0除以4时,您将得到0。为了解决这种情况,您还需要一个规则。

Also don't forget that with your current code if the total amount of posts is for instance 12, you will have one empty "frak" div at the end of your posts. 同样不要忘记,如果您的当前代码的帖子总数为例如12,则在帖子末尾会有一个空的“ frak” div。

<?php 
    $i = 0;
    $wrap_div = "<div class='frak'>";
        if ( have_posts() ) {
            // Grab the total posts that are being displayed
            $total_posts = $wp_query->post_count;
            echo $wrap_div;
            while ( have_posts() ) {
                the_post(); ?>
                <div class="wine-section">
                    <?php the_post_thumbnail(); ?>
                    <div class="wine-info">
                        <a href="<?php the_permalink(); ?>"><?php the_title( '<h1>', '</h1>' ); ?></a>
                        <?php the_meta(); ?>
                    </div><!-- /wine-info -->
                    <div class="c"></div>
                </div><!-- /wine-section -->
                <?php 
                // Is this a fourth post? If so, make sure it is not the last post?
                if ( $i % 4 == 0 && $i != 0 && ( $i + 1 ) != $total_posts ) {
                    echo '</div>' . $wrap_div;
                }
                $i ++;
            } // end while
            // Close the $wrap_div
            echo '</div>';
        } // end if
?>

As you can see the if statement that prints the closing tag and the new wrap is more complex now. 如您所见,打印结束标记和新换行的if语句现在更加复杂。 It makes sure that $i is not 0(meaning it's still the first post) and that $i + 1 does not equal the total amount of posts displayed(for that case we close after the while() loop). 它确保$ i不为0(意味着它仍然是第一条帖子),并且$ i + 1不等于显示的帖子总数(在这种情况下,我们在while()循环之后关闭)。

If you're wondering why we're closing after the while() loop - simply because your posts might not always be exactly multiple on 4(I'm not sure about the correct translation to English here) - and if that's case and you don't close your div after the while loop - you'll have troubles. 如果您想知道为什么我们在while()循环后关闭-仅仅是因为您的帖子可能并不总是在4上完全是多(我不确定此处是否正确翻译成英语)-如果是这样,您在while循环后不要关闭div-会有麻烦。

You are incrementing $i outside the while loop, so inside it $i will always be ==0 and thus $i % 4 == 0 . 您在while循环外递增$i ,因此在其中$i总是==0 ,因此$i % 4 == 0

Move $i++; 移动$i++; before } // end while . } // end while之前} // end while

However, you should also change your condition to $i % 4 == 3 , because $i % 4 == 0 evaluates to true in the very first iteration ( $i=0 ) and will produce an initial <div> with only one post. 但是,您还应该将条件更改为$i % 4 == 3 ,因为$i % 4 == 0在第一次迭代( $i=0 )中计算为true ,并且将生成仅包含一个的初始<div>帖子。

Alternatively, you could keep your condition as it is, and: 或者,您可以保持现状,并且:

  • either start with $i=1 instead of 0 $i=1而不是0开头

  • or move $i++ right after while . 或在while之后立即移动$i++

Now, when you have an exact multiple of 4 posts, an extra, empty <div> will appear at the end. 现在,当您有4个帖子的确切倍数时,结尾处会出现一个额外的空<div> That's because you're assuming that, when a <div> closes, another one should automatically open. 那是因为您假设<div>关闭时,另一个应该自动打开。 That's not always the case. 并非总是如此。

Assuming that you have chosen the $i % 4 == 3 solution above, and that you already have a echo '</div>'; 假设您已经选择了上面的$i % 4 == 3解决方案,并且已经有了echo '</div>'; after the while loop, change your condition to if(($i % 4 == 3)&& have_posts()) . while循环之后,将条件更改为if(($i % 4 == 3)&& have_posts())

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

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