简体   繁体   English

在X次后包装WordPress帖子,而不修改循环?

[英]Wrapping WordPress Posts After X Amount of Times, Without Modifying Loop?

I have a WordPress starter theme and one of the features is the ability to chose different archive formats by selecting different template parts. 我有一个WordPress入门主题,其功能之一就是能够通过选择不同的模板部件来选择不同的存档格式。 My index.php essentially looks like this: 我的index.php本质上是这样的:

            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                <!-- To see additional archive styles, visit the /parts directory -->
                <?php get_template_part( 'parts/loop', 'archive' ); ?>

            <?php endwhile; ?>  

                <?php joints_page_navi(); ?>

            <?php else : ?>

                <?php get_template_part( 'parts/content', 'missing' ); ?>

            <?php endif; ?>

One of the archive formats is a grid format, which essentially needs to output like so: 存档格式之一是网格格式,本质上需要像这样输出:

Start Row
    Post 1
    Post 2
    Post 3
End Row
Start Row
    Post 4
    Post 5
    Post 6
End Row
.....

Normally, I use this method: 通常,我使用这种方法:

<?php foreach (array_chunk($posts, 2, true) as $posts) :  ?>

    <div class="row">

        <?php foreach( $posts as $post ) : setup_postdata($post); ?>

            <div class="six columns"> 
                <?php the_content(); ?>
            </div>

    <?php endforeach; ?>

    </div>

<?php endforeach; ?>

However, that piece of code requires a different type of loop than the standard WP loop, which makes it hard to integrate into the theme without the user having to also make adjustments to the loop. 但是,这段代码需要的循环类型与标准WP循环不同,这使得很难将其集成到主题中,而无需用户也必须对该循环进行调整。

So my question is, is it possible to wrap X amount of posts with a div without modifying the standard WordPress loop? 所以我的问题是,是否可以在不修改标准WordPress循环的情况下用div包装X个帖子?

You can start iterating (counting) over posts by using the iterator $i ; 您可以使用迭代器$i开始迭代(计数)帖子。

In your archive.php or index.php (the file which has the main query): 在您的archive.phpindex.php (具有主要查询的文件)中:

<?php if (have_posts()) :
    $i = 1; //Start counting

    while (have_posts()) : the_post(); ?>

    <!-- To see additional archive styles, visit the /parts directory -->
    <?php get_template_part( 'parts/loop', 'archive' ); ?>

    <?php $i++; //Increase $i ?>

    <?php endwhile; ?>  

    <?php joints_page_navi(); ?>

<?php else : ?>

    <?php get_template_part( 'parts/content', 'missing' ); ?>

<?php endif; ?>

And in your parts/loop file (which has the loop " <article></article> "), make calculations to check the current post index and decide whether to skip, start or close the wrapper tag: 然后在您的parts/loop文件(具有循环“ <article></article> ”)中,进行计算以检查当前的文章索引并确定是跳过,启动还是关闭包装器标签:

<?php
//Doing some math           
$x = 4 //Change this to any number you want
if ( $i == 1 || $i == $x || $i % $x == 1 ) {
    $before_article = '<div class="wrapper">'; //The wrapper start tag
}
if ( $i % $x == 0 || $wp_query->current_post + 1 == $wp_query->post_count ) {
    $after_article = '</div>'; //The wrapper end tag
}

<?php echo $before_article; ?>
<article>
    <!-- post content here -->
</article>
<?php echo $after_article; ?>
<?php $before_article = ''; $after_article = ''; ?>

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

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