2013-11-26 1 views
0

게시물을 페이지에 올리는 데 문제가 있습니다. 특정 카테고리의 게시물을 가져 오려고하지만 게시물이로드되면로드가 5 번 표시됩니다. 나는 이미 관리 패널에서 볼 수있는 게시물의 수를 변경하려했지만 게시물의 출력에는 영향을 미치지 않습니다. 사전에쿼리 게시물에서 뉴스 항목을 5 번 출력합니다.

<div id="primary" class="content-area"> 
    <main id="main" class="site-main" role="main"> 

     <?php 

     query_posts(array ('category_name' => 'nieuwsitem', 'posts_per_page' => 20)); 

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

     <?php $myposts = get_posts(''); 
       foreach($myposts as $post) : 
       setup_postdata($post); 
       ?> 
       <div class="post-item"> 
        <div class="thedate"><?php echo get_the_time('d/m/Y', $post->ID); ?></p></div> 
        <div class="post-info"> 
        <h2 class="post-title"> 
        <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"> 
        <?php the_title(); ?> 
        </a> 
        </h2> 
        </div> 
        <div class="post-content"> 
        <?php the_content(); ?> 
        </div> 
       </div> 
       <?php endforeach; wp_reset_postdata(); ?> 

     <?php endwhile; // end of the loop. ?> 

    </main><!-- #main --> 
</div><!-- #primary --> 

감사 :

이 내 코드입니다!

+0

. 'get_posts'는 루프를 사용할 필요가 없습니다. 여기에서는 기본 쿼리를 수정하고 해당 게시물 ('while')을 반복하며 각 루프 내에서 각 동작을 가져 오는 각 게시물을 출력하는 다른 foreach 루프를 실행합니다. 그냥 대신 새로운 WP_Query와 간단한 WordPress 루프를 사용하면됩니다. – Ennui

+0

'WP_Query'로 구현 된 것에 대한 답을 참조하십시오. – Ennui

답변

1

두 개의 루프를 만들었습니다. 코드의 foreach는 부분을 제거하면이 문제를 해결해야합니다

<div id="primary" class="content-area"> 
<main id="main" class="site-main" role="main"> 

    <?php 

    query_posts(array ('category_name' => 'nieuwsitem', 'posts_per_page' => 20)); 

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

      <div class="post-item"> 
       <div class="thedate"><?php echo get_the_time('d/m/Y', $post->ID); ?></p></div> 
       <div class="post-info"> 
       <h2 class="post-title"> 
       <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"> 
       <?php the_title(); ?> 
       </a> 
       </h2> 
       </div> 
       <div class="post-content"> 
       <?php the_content(); ?> 
       </div> 
      </div> 

    <?php endwhile; // end of the loop. ?> 

</main><!-- #main --> 

0

보십시오

$ args 배열 ('CATEGORY_NAME'=> 'nieuwsitem', 'posts_per_page'=> 20);

$ myposts = get_posts ($ args);

및 제거 :

동안 (have_posts을()) : the_post를(); ?>

endwhile;

나는 두 개의 쿼리로 게시물을 쿼리 할 때마다 다음과 같은 게시물을 얻을 것이라고 생각합니다. while (have_posts()) : the_post();

다음과 같이 게시물을 다시 쿼리합니다. $ myposts = get_posts ('');

1

이 그것을 할 수있는 적절한 방법입니다. 대부분의 상황에서는 query_postsget_posts의 사용을 피해야하며 여기서 WP_Query이 더 적절합니다.

당신이 그 쿼리에서 루프 당신 get_posts 안에 다음, 해당 범주 게시물을 얻을 foreach를 통해 그들 모두를 표시하는 메인 쿼리를 수정하기 때문에 각 게시물의 여러 사본을 받고, 그래서 당신은 모든 출력되는 이유 중첩 된 루프로 인해 해당 카테고리의 모든 게시물에 대해 해당 카테고리에 게시하십시오. get_posts은 루프와 함께 사용할 수 없습니다.

이 당신을 위해 무엇을해야 : 당신은`이것에 대한 query_posts`와`get_posts`을 하나의`WP_Query`을하지 사용해야

<div id="primary" class="content-area"> 
    <main id="main" class="site-main" role="main"> 

     <?php 

     $catquery = new WP_Query('category_name=nieuwsitem&posts_per_page=20'); 
     if ($catquery->have_posts()) : 
     while ($catquery->have_posts()) : $catquery->the_post(); ?> 

       <div class="post-item"> 
        <div class="thedate"><?php echo get_the_time('d/m/Y', $post->ID); ?></p></div> 
        <div class="post-info"> 
        <h2 class="post-title"> 
        <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"> 
        <?php the_title(); ?> 
        </a> 
        </h2> 
        </div> 
        <div class="post-content"> 
        <?php the_content(); ?> 
        </div> 
       </div> 
     <?php endwhile; // end of the loop. 
     else : 
      echo '<h3>No posts found.</h3>'; 
     endif; ?> 

    </main><!-- #main --> 
</div><!-- #primary --> 
관련 문제