프로젝트 : 나는 두 개의 루프를 처리하고있어 단일 쿼리로WP_Query()
카운트 총 두 개의 루프 내에서 항목 - 루프 내 루프
- 나는 루프에서 루프를 호출 워드 프레스 프로젝트
쿼리. 구조는 다음과 같습니다.
<?php
while($my_query -> have_posts()) :
$my_query -> the_post();
if(condition) { ?>
<div class="grid-box">
<div class="item">Item of this kind</div>
</div> <!-- .grid-box -->
<?php
}
if(condition) {
$someCounter = grab some counter here;
for($inner = 0; $inner < $someCounter; $inner ++) {
?>
<div class="grid-box">
<div class="item">Item of that** kind#<?php echo $inner; ?></div>
</div> <!-- .grid-box -->
<?php
} //end for
}
endwhile;
?>
CSS를 사용하면 멋진 그리드 블록에 항목을 표시하는 쿼리가 훌륭하게 수행됩니다. 그러나 행이 아닌 항목이 많을수록 두 번째 행의 항목이 첫 번째 항목과 충돌합니다. 그래서 다음과 같이 행 클래스에 넣을 계획이었습니다.
<div class="row">
<!-- every 6 items within a grid -->
<div class="grid grid-first>Item of this kind</div>
<div class="grid>Item of this kind</div>
<div class="grid>Item of that** kind</div>
<div class="grid>Item of that** kind</div>
<div class="grid>Item of this kind</div>
<div class="grid grid-last>Item of that** kind</div>
</div>
<div class="row">
<div class="grid grid-first>Item of that** kind</div>
<div class="grid>Item of that** kind</div>
<div class="grid>Item of this kind</div>
</div>
이제 총 항목 수를 계산해야합니다. 어떻게해야합니까? 두 개의 카운터를 전달해야합니까? 그렇다면 정확한 카운트를 계산하고 그 카운트를 조건으로 사용하여 .row
과 함께 div를로드하는 방법은 무엇입니까? 필자가 다루고있는 것으로서, $inner
카운터가 동적 코드에 중요합니다. 그러나 우리는 총 카운트에 카운트를 사용할 수 있습니다.
제 동안 루프를 시작하기 전에, '$ intTotal = 0으로 초기화;', 증분 '$ intTotal를 ++,'내부 장치 및 출력 '인쇄의 sprintf - 루프 ("% d의 전체 행'$ intTotal) ; 당신의 마지막 뒤에. – SaschaM78