2014-11-29 4 views
1

이 방법은 Wordpress 포럼에 게시 된 사람의 ID (순차적으로 주문하지 않음)에서 "number"(1, 2, 3, 4 등) . 슬라이드에 대해서만 쿼리를 수정했습니다.Wordpress에서 게시물을 열거하면 두 번 작동하지 않습니다

function Get_Post_Number($postID){ 
    $temp_query = $wp_query; 
    $postNumberQuery = new WP_Query(array(
     'ignore_sticky_posts' => 1, 
     'posts_per_page' => 20, 
     'post_type' => 'slide')); 

    $counter = 0; 
    $postCount = 0; 
    if($postNumberQuery->have_posts()) : 
     while ($postNumberQuery->have_posts()) : $postNumberQuery->the_post(); 
      if ($postID == get_the_ID()){ 
       $postCount = $counter; 
      } else { 
       $counter++; 
      } 
    endwhile; endif; 
    wp_reset_query(); 
    $wp_query = $temp_query; 
    return $postCount; 
} 

한 번 호출하면 올바르게 작동하고 올바른 번호를 반환합니다.

Get_Post_Number(get_the_ID()) //Returns 3 (for instance) 

그러나 두 번 호출 할 때, 그것은 더 이상 작동 :이 기능은 부작용을 가지고 있다고 생각하는 날 리드하지만, 내가 그들을 취소하는 방법을 잘 모르겠어요

Get_Post_Number(get_the_ID()) //Returns 3 (for instance) 
Get_Post_Number(get_the_ID()) //Returns 0 (wrong!) 

. Wordpress는 자신 만의 "사용자 정의 루프"를 수행 한 후에 wp_reset_query()으로 전화를 걸지 만 기능은 이미 그렇게합니다.

어떻게해야합니까?

+0

여러 번 전화를 걸거나 다른 번호를 사용할 때 항상 0을 반환합니까? – anthonygore

+0

@anthonygore 그냥 0. –

+0

'$ postID == get_the_ID()'이 맞다면 $ 카운터 변수는 항상 0이 될 것입니다. 테스트 할 양쪽 vars를 echo하지만 id는 무슨 일이 일어나는지 말합니다 – David

답변

0

방금 ​​wp_reset_query()이 삭제되어 작동했습니다. (비록 Wordpress가 전화하라고 알려주지 만!)

관련 문제