我是word press的新手,我正在处理帖子,我的管理面板中总共有10篇帖子,我试图通过他们的ID获取帖子,但它输出了最新的5篇帖子。
这是我的代码示例。我不知道我哪里做错了,如果你能给我一点提示,我将不胜感激。
<?php
$thePostIdArray = array("11", "13", "15", "17", "19");
$limit = 5;
if ( have_posts() ) : while ( have_posts() ) : the_post(); $counter++;
if ( $counter < $limit + 1 ) : $post_id = $thePostIdArray[$counter-1]; $queried_post = get_post($post_id);
?>
<a href="/applications">
<div class="col-md-5 apps text-center">
<?php the_post_thumbnail(\'full\', array( \'class\' => \'img-responsive\' )); ?>
<div class="caption">
<h3><?php echo $queried_post->post_title; ?></h3>
<button type="button" class="btn btn-default home-page-explore hvr-grow-shadow">
Explore
</button>
</div>
</div> </a>
<?php endif; endwhile; endif; ?>
请看,我已经在数组中指定了ID,但它没有显示这些ID,而是显示了amdin面板中最近的5篇文章。
最合适的回答,由SO网友:Aniruddha Gawade 整理而成
Try this:
<?php
$thePostIdArray = array("11", "13", "15", "17", "19");
$limit = 5;
$query=new WP_Query(array(\'post__in\'=>$thePostIdArray));
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); $counter++;
if ( $counter < $limit + 1 ) : $post_id = $thePostIdArray[$counter-1]; $queried_post = get_post($post_id);
?>
<a href="/applications">
<div class="col-md-5 apps text-center">
<?php the_post_thumbnail(\'full\', array( \'class\' => \'img-responsive\' )); ?>
<div class="caption">
<h3><?php echo $queried_post->post_title; ?></h3>
<button type="button" class="btn btn-default home-page-explore hvr-grow-shadow">
Explore
</button>
</div>
</div> </a>
<?php endif; endwhile; endif; ?>