2014-03-25 148 views
0

我想通过id获取帖子。 Id的数组。我正在使用此代码,但现在正在工作。通过id在wordpress中获得帖子

$the_query = new WP_Query(array( 
    'post_type' => 'job_listing', 
    'post__in' => array(311, 312) 
)); 

print_r($the_query); //this doesn't print any data 

if ($the_query->have_posts()) { 
    echo '<ul>'; 
    while ($the_query->have_posts()) { 
     $the_query->the_post(); 
     echo '<li>' . get_the_title() . '</li>'; 
    } 
    echo '</ul>'; 
} 
+0

那么,当运行时没有任何显示? – Howli

+0

'print_r($ the_query);'不打印**事实**真的很奇怪......你是否在你的代码中使用了命名空间? – MikO

+0

已经做到了,谢谢你的时间 – Riz

回答

0

您可以使用get_posts(),因为它需要相同的参数WP_Query

要传递它的ID,使用'post__in' => array(311, 312)(只需要数组)。

下面是例子。

$args = array(
    'post_type' => 'job_listing', 
    'post__in' => array(311, 312) 
); 

$posts = get_posts($args); 

foreach ($posts as $p) : 
    //post! 
endforeach; 
相关问题