网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

Wordpress循环计数器问题

GG网络技术分享 2025-03-18 16:12 5


问题描述:

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

}

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我有这段代码可以通过自定义的帖子类型客户端... </ p>

\\ n

 &lt; div class =“wrap”style =“padding-top:0!important;”&gt; 

&lt;?php

$ args = array(

\'post_type\'= &gt;\'short_courses\',

//\'orderby\'=&gt;\'none\',

\'course_type\'=&gt;\'digital-marketing\'

);

$ the_query = new WP_Query($ args); \\ n?&gt;

&lt;?php if(have_posts()):while($ the_query-&gt; have_posts()):$ the_query-&gt; the_post(); ?&gt;

&lt; div class =“1/3 course-item”&gt;

&lt; a href =“&lt;?php the_permalink();?&gt;”&gt;

&lt; p&gt;&lt; img src =“&lt;?php the_field(\'listing_thumbnail\');?&gt;”&gt;&lt; / p&gt;

&lt; h4&gt;&lt;?php the_title(); ?&GT;&LT; / H4&GT;

&lt; p style =“color:#f08464;”&gt;&lt;?php the_field(\'date_for_thumbnail_listing\'); ?&GT;&LT; / p为H.

&lt; p&gt;&lt;?php the_field(\'price\'); ?&gt;&lt; / p&gt;

&lt; a href =“&lt;?php the_permalink();?&gt;” class =“sc-box-button”style =“background-color:#f08464; margin-bottom:40px;”&gt; MORE INFO&lt; / a&gt;

&lt; / a&gt;

&lt; / div&gt;

&lt;?php $ products_count = $ the_query-&gt; current_post + 1; ?&gt;

&lt;?php if($ products_count%4 == 0):?&gt;

&lt;?php endif; ?&gt;

&lt;?php endwhile; wp_reset_postdata(); 万一; ?&gt;

&lt; / div&gt;

</ code> </ pre>

一切正常,但位于底部:</ p>

 &lt;?php $ products_count = $ the_query-&gt; current_post + 1;  ?&gt; 

&lt;?php if($ products_count%4 == 0):?&gt;

</ code> </ pre>

我正在尝试让项目流入 4个项目之后的下一行...目前正在3个项目之后进行... </ p>

道歉如果它很简单我就错过!!! </ p>

***** UPDATE FIXED ****** </ p>

抱歉 - 我用CSS实现了这个效果,道歉但是我只是将CSS元素更改为 :</ p>

  @media(min-width:576px){

.card-columns {

column-count:4; }

}

</ code> </ pre>

</ div>

网友观点:

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

###

I would recommend doing this before the while:

$products_count = 0;

And this for counting:

$products_count++;

if ( $products_count % 4 == 0): ...

###

<?php $products_count = $the_query->current_post; ?>

Remove only +1 from the above code.

标签:

提交需求或反馈

Demand feedback