2017-02-09 1 views
2

나는 내 Wordpress Index (index.php) 페이지에서이 루프를 실행하는데, 문제없이 내 게시물을 표시한다.내 Wordpress Query가 비 인덱스 페이지에서 작동하지 않는 이유는 무엇입니까?

하지만이 코드를 다른 페이지에서 사용하려고하면 게시물이 없습니다.

이유를 설명 할 수 있습니까?

Ta!

<?php 
     $args = array(
      'post_type' => 'post', 
       ); 
     $query = new WP_Query($args); 
     if ($query->have_posts()) : 
      // Start the Loop. 
      while (have_posts()) : the_post(); 
       get_template_part('content', get_post_format()); 

      endwhile; 
      // Previous/next post navigation. 
      twentyfourteen_paging_nav(); 

     else : 
      get_template_part('content', 'none'); 

     endif; 
    ?> 
+0

이 템플릿의 이름은 무엇입니까 ?? –

답변

1

그냥 몇 가지 중요한 포인트 :

추가하십시오 wp_reset_postdata(); 모든 맞춤 검색어 후에 https://codex.wordpress.org/Function_Reference/wp_reset_postdata

또한 게시 된 게시물 만 쿼리하십시오. 'post_status'=> '게시'

<?php 
     $args = array(
      'post_type' => 'post', 
      'posts_per_page' => -1, 
      'post_status' => 'publish' 
       ); 
     $query = new WP_Query($args); 
     if ($query->have_posts()) : 
      // Start the Loop. 
      while ($query->have_posts()): 
       $query->the_post(); 
       get_template_part('content', get_post_format()); 
      endwhile; 
      // Previous/next post navigation. 
      twentyfourteen_paging_nav(); 

      // Reset post data 
      wp_reset_postdata(); 

     else : 
      get_template_part('content', 'none'); 

     endif; 
    ?> 
+0

아아 그래서 저는 '$ query->; 내 while 루프에서. 좋은 연습을위한 포인터에 감사드립니다. – jack

1

코드 수정이 거의 필요 없습니다. 시도해보십시오. 나를 위해 일했다. 여기

<?php 
     $args = array(
      'post_type' => 'post', 
      'posts_per_page' => -1 
       ); 
     $query = new WP_Query($args); 
     if ($query->have_posts()) : 
      // Start the Loop. 
      while ($query->have_posts()): 

     $query->the_post(); 
       get_template_part('content', get_post_format()); 
          endwhile; 
      // Previous/next post navigation. 
      twentyfourteen_paging_nav(); 

     else : 
      get_template_part('content', 'none'); 

     endif; 
    ?> 
+0

감사합니다 anjana, 당신이 바로이 매력을 일한거야! – jack

+0

Jack @jack과 upvote도 감사합니다 :) –

관련 문제