programing

워드프레스 루프 - 항목 카운트 방법

easyjava 2023. 3. 20. 23:41
반응형

워드프레스 루프 - 항목 카운트 방법

Wordpress 루프 코드 내에서 여러 항목을 가져올 수 있는 방법이 있습니까?

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

이 루프에는 투고가 나열됩니다. 나는 전체 수에 따라 처음 세 개에 특정 클래스를 추가해야 합니다.

속성을 사용할 수 있습니다.$WP_Query다음과 같은 경우:

$wp_query->post_count

와의 차이점에 주의해 주세요.found_posts쿼리와 일치하지만 표시되지 않는 게시물을 카운트합니다(페이지 번호 부여 등).특정 상황에 따라 둘 중 하나를 사용하는 것이 좋을 수 있습니다.

한 가지 방법은 다음과 같습니다.

<?php 
 $count = 0; //set up counter variable
 while (have_posts()) : the_post(); 
 $count++; //increment the variable by 1 each time the loop executes
 if ($count<4) {
    // here put the special code for first three
 }
 // here put the code for normal posts
 endwhile;
 ?>

나는 이것을 내 것에 사용했다.

<?php $count = 0;
  if ( have_posts() ) : while ( have_posts() ) : the_post(); $count++;?>
        <div  class="col-lg-3">
            <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
            <p><?php the_excerpt();?></p>
        </div>

<?php if ($count==4) { $count = 0;?>
        <div class="clearfix"></div>
<?php } ?>

<?php endwhile; endif; ?>

가장 쉬운 것은wc_get_loop_prop( 'total' )

예를 들어 템플릿으로 가져올 경우 루프 변수에 액세스할 수 없습니다.

언급URL : https://stackoverflow.com/questions/19303556/wordpress-loop-how-to-count-items

반응형