2013-11-27 7 views
0

프로젝트 : 나는 두 개의 루프를 처리하고있어 단일 쿼리로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 카운터가 동적 코드에 중요합니다. 그러나 우리는 총 카운트에 카운트를 사용할 수 있습니다.

+0

제 동안 루프를 시작하기 전에, '$ intTotal = 0으로 초기화;', 증분 '$ intTotal를 ++,'내부 장치 및 출력 '인쇄의 sprintf - 루프 ("% d의 전체 행'$ intTotal) ; 당신의 마지막 뒤에. – SaschaM78

답변

0

카운트를 들어 당신은 wp_count_posts() 함수를 호출 한 후 '게시'속성에 액세스 것, 게시 된 상태 유형을 얻으려면이

<?php wp_count_posts($type, $perm); ?> 

처럼 사용할 수 있습니다.

<?php 
$count_posts = wp_count_posts(); 

$published_posts = $count_posts->publish; 
?> 

페이지 수 상태 유형 계산은 게시물과 동일한 방식으로 이루어지며 첫 번째 매개 변수를 사용합니다. 게시물 상태에 대한 게시물 수 찾기는 게시물의 경우와 같은 방식으로 수행됩니다.

<?php 
$count_pages = wp_count_posts('page'); 
?> 
+0

'wp_count_posts()'는 내부 루프 (for 루프) 항목이 아닌 메인 루프의 게시물 만 계산하므로 여기에 적합하지 않습니다. –