2016-08-24 1 views
0
 <?php 
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
     $args = array(
      'post_type' => 'post', 
      'posts_per_page' => 7, 
      'paged' => $paged); 
     $wp_query = new WP_Query($args); 
     $count = 0; 

     while (have_posts()) : the_post(); ?> 
      <?php echo get_the_title(); ?> 
      <?php 
       $count++; 
       if ($count === 1) { ?> 
        <div class="column small-12 margin-bottom-small"> 
         <div class="panel-brown"> 
          <img src="" alt="" /> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div class="para-white"> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
       while ($count <= 2) { 
        $count++ ?> 
        <div class="column small-12 medium-6 margin-bottom-small"> 
         <div class="panel-tan"> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
       while ($count <= 4) { 
        $count++ ?> 
        <div class="column small-12 medium-6 margin-bottom-small"> 
         <div class="panel-tan"> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
      ?> 
     <?php endwhile; ?> 

     <!-- Pagination links --> 
     <?php next_posts_link('&larr; Older posts', $wp_query ->max_num_pages); ?> 
     <?php previous_posts_link('Newer posts &rarr;'); ?> 

왜 여부와 문은 같은 게시물을 반복하지만 동안 직접이 줄 끝에서 나는PHP - 동일한 포스트 대신 다음 게시물의 게재되고 이해하기 위해 사투를 벌인거야

while (have_posts()) : the_post(); 

제목을 되풀이하여 대신 게시물 목록을 표시합니다. 내가 여기 달성하기 위해 노력하고 무엇

루프에서 출력되는 각 게시물에 대해 다른 HTML 레이아웃을 만들 수 있습니다.

건배

+0

당신이 첫 번째 문을 의미합니까 당신의 노력들에 대한

감사합니다''$ 동안> = 2'를 계산? if 문은 모두 if 문이어야합니다. 또한 메인 루프에서 WP_Query의 결과를 어떻게 사용하고 있는지 보지 못했을 것입니다 ... 아마도 손에있는 문제 – Chizzle

답변

1

나는 WP_Query official documentation에서와 같이 쿼리 결과를 통해 루프하려고하는 것 같아요.

또한, 당신과 같은 루프의 주요 포스트 루프 자체 내부에있는 동안 당신이 왜 확실하지가 그래서 if 문에 사람들을 변경했습니다.

이 같은

뭔가 당신이 믿는 것입니다 후 :

<?php 
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
     $args = array(
      'post_type' => 'post', 
      'posts_per_page' => 7, 
      'paged' => $paged); 
     $wp_query = new WP_Query($args); 
     $count = 0; 

     if(! $wp_query->have_posts()) 
      die('no posts!'); // ADDED - Handle this more elegantly 

     while ($wp_query->have_posts()) : $wp_query->the_post(); // ADDED - Use query result object?> 
      <?php echo get_the_title(); ?> 
      <?php 
       $count++; 
       if ($count === 1) { ?> 
        <div class="column small-12 margin-bottom-small"> 
         <div class="panel-brown"> 
          <img src="" alt="" /> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div class="para-white"> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
       if ($count <= 2) { 
        ?> 
        <div class="column small-12 medium-6 margin-bottom-small"> 
         <div class="panel-tan"> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
       if ($count <= 4) { // ADDED - Be careful, this will fire as well as the if statement above it since they are both <= 2 
        ?> 
        <div class="column small-12 medium-6 margin-bottom-small"> 
         <div class="panel-tan"> 
          <h5 class="heading-white"><a href="#"><?php echo get_the_title(); ?></a></h5> 
          <div> 
           <?php $content = get_the_content(); echo mb_strimwidth($content, 0, 200, '...');?> 
          </div> 
         </div> 
        </div> 
      <?php } 
      ?> 
     <?php endwhile; ?> 

     <!-- Pagination links --> 
     <?php next_posts_link('&larr; Older posts', $wp_query ->max_num_pages); ?> 
     <?php previous_posts_link('Newer posts &rarr;'); ?> 
+0

while 루프를 사용하려고 했으므로 코드를 줄이기 위해 두 개의 if 문을 사용할 필요가 없었습니다. . 이 솔루션이 작동하는지 살펴 보도록하겠습니다. 감사합니다. –

+0

죄송합니다. 아직 모든 게시물을 반복하지 않고 동일한 게시물을 출력하고 있습니다. –

+0

아, 알아 냈어. –

0

나는 그것이 바로 카운트에 있던 결코 의미 while 루프에서 여러 장소에서 수를 ++ 호출하기 때문에이 작동하지 않는 이유는 . 나는 이것을 if와 while 루프 바깥에 한 번 넣음으로써 이것을 고쳤다. $ 카운트 === 1`하지만 나중에이있는 경우

관련 문제