doujiang1913 2018-01-10 12:50
浏览 73
已采纳

Wordpress循环计数器问题

I have this piece of code thats pulling through a custom post type clients...

<div class="wrap" style="padding-top: 0 !important;">

    <?php
        $args = array( 
          'post_type' => 'short_courses',
          // 'orderby' => 'none',
          'course_type' => 'digital-marketing'
        );
        $the_query = new WP_Query( $args );
    ?>

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


        <div class="one-third course-item">
            <a href="<?php the_permalink(); ?>">

                <p><img src="<?php the_field('listing_thumbnail'); ?>"></p>
                <h4><?php the_title(); ?></h4> 
                <p style="color: #f08464;"><?php the_field('date_for_thumbnail_listing'); ?></p>  
                <p><?php the_field('price'); ?></p>
                <a href="<?php the_permalink(); ?>" class="sc-box-button" style="background-color: #f08464; margin-bottom: 40px;">MORE INFO</a>

            </a>
        </div>

    <?php $products_count = $the_query->current_post + 1; ?>
    <?php if ( $products_count % 4 == 0): ?>

    <?php endif; ?>


    <?php endwhile; wp_reset_postdata(); endif; ?>

</div>

All working great, but with the bit at the bottom:

<?php $products_count = $the_query->current_post + 1; ?>
<?php if ( $products_count % 4 == 0): ?>

I'm trying to get the items to flow onto the next line after 4 items... currently it's doing it after 3 items...

Apologies if it's something simple I'm missing!!!

***** UPDATE FIXED ******

Sorry - I had achieved this effect with CSS, apologies but it was just down to me changing the CSS element to:

@media (min-width: 576px) {
  .card-columns {
    column-count: 4; }
}
  • 写回答

3条回答 默认 最新

  • douyi2664 2018-01-10 12:57
    关注

    2 ways to do that:-

    1.Either Add counter befor loop and then increment it and check(standered+best way)

    $products_count = 0;
    <?php if ( have_posts() ) :
      //rest code as it is
    <?php $products_count++; ?>
    <?php if ( $products_count % 4 == 0): ?> //rest code as it is
    

    2.Or Remove +1 from <?php $products_count = $the_query->current_post; ?>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'
  • ¥15 vue+element项目中多tag时,切换Tab时iframe套第三方html页面需要实现不刷新
  • ¥50 深度强化学习解决能源调度问题