반응형
워드프레스 루프 - 항목 카운트 방법
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
반응형
'programing' 카테고리의 다른 글
| 페이지 새로 고침 없이 양식 제출 (0) | 2023.03.20 |
|---|---|
| 반응 - useState 초기값이 설정되지 않음 (0) | 2023.03.20 |
| 현재 사용자에게 활성 구독이 있는지 감지 (0) | 2023.03.20 |
| ChunkLoadError: 청크 노드_modules_next_dist_client_dev_noop_js를 로드하지 못했습니다. (0) | 2023.03.20 |
| react-native - 전체 화면 크기가 아닌 View를 포함하는 이미지에 맞춥니다. (0) | 2023.03.20 |