2017-07-24 77 views
1

我在写一个wp主题,因为我只是想围绕WP_Query做一个包装函数并重用它。为了简化生活。WP_Query功能不正确

我的包装功能是:

//for making wp_query easily 
    function myQuery($options,$callback){ 
     $the_query = new WP_Query($options); 
     if ($the_query->have_posts()) { 
      while ($the_query->have_posts()) { 
       $the_query->the_post(); 
       $callback(); 
      }    
     } 
     wp_reset_postdata(); 
     wp_reset_query(); 
    } 


//get a single post by the id. 
function get_post_by_id($id,$callback){  
    $options = array('post_in' => $id,); 
    myQuery($options,$callback); 
} 

但是当我使用get_post_by_id()功能,我得到意想不到的结果,我注意到,回调被称为多的时间。

我该如何解决这个问题?

回答

0

您应该使用的正确选项是post__in而不是post_in。由于您没有发送正确的选项,因此wp_query会回应所有帖子,因此会回调多个回调呼叫。

+0

那么为什么没有包装功能它的工作????????? –

+0

那么,你试过'post_in'吗?它有用吗?如果你没有使用包装函数,你在哪里/如何调用这些代码? –