dongyue5686 2015-11-22 04:32
浏览 24
已采纳

在没有修改循环的情况下,在X次数之后包装WordPress帖子?

I have a WordPress starter theme and one of the features is the ability to chose different archive formats by selecting different template parts. My index.php essentially looks like this:

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

So my question is, is it possible to wrap X amount of posts with a div without modifying the standard WordPress loop?

  • 写回答

1条回答 默认 最新

  • dongxie559554 2015-11-22 04:58
    关注

    You can start iterating (counting) over posts by using the iterator $i;

    In your archive.php or index.php (the file which has the main query):

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

    <?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 = ''; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c