PHP WordPress按ID获取帖子,并在每个页面上制作特色部分


php wordpress get post by id and make featured section on every page

我最近编写了一个特色部分,该部分实际上将拉取数组中指定的帖子。这是代码

<div class="container_24">
<ul id="featured_posts" class="grid_24">
<?php $thePostIdArray = array("13968","14293", "15018", "15512"); ?>
<?php $limit = 4 ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); $counter++; ?>
<?php if ( $counter < $limit + 1 ): ?>
<?php $post_id = $thePostIdArray[$counter-1]; ?>
<?php $queried_post = get_post($post_id); ?>
 <li class="featured_post">
    <div class="featured_thumbnail"> <a href="<?php echo get_permalink( $post_id ); ?>"><img width="55" height="55" src="<?php echo img_feature($post_id) ?>" class="attachment-featured-small wp-post-image" alt="<?php echo $queried_post->post_title; ?>"></a></div>
    <div class="featured_title">
      <h2><a href="<?php echo get_permalink( $post_id ); ?>"><?php echo $queried_post->post_title; ?></a></h2>
    </div>
  </li>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div>

现在,此代码在homearchive和任何具有post循环的页面上完美运行,并且它显示了4假定的特色帖子,但是问题它没有显示在任何其他没有post循环的页面上,例如single.phpcontact page以及任何其他页面,例如没有帖子的单个页面。

我不知道我是否在上面做错了什么,我已经把它包含在header上,它应该显示到包含header的所有页面,问题只显示第一个,而不是其余的就像循环只去一次并停止。

如果我做错了什么,请指出我。

谢谢

使用如下所示

的 foreach 循环。

<div class="container_24">
<ul id="featured_posts" class="grid_24">
<?php $thePostIdArray = array("13968","14293", "15018", "15512"); ?>
<?php foreach ($thePostIdArray as $post_id) : ?>
    <?php $queried_post = get_post($post_id); ?>
     <li class="featured_post">
        <div class="featured_thumbnail"> <a href="<?php echo get_permalink( $post_id ); ?>"><img width="55" height="55" src="<?php echo img_feature($post_id) ?>" class="attachment-featured-small wp-post-image" alt="<?php echo $queried_post->post_title; ?>"></a></div>
    <div class="featured_title">
        <h2><a href="<?php echo get_permalink( $post_id ); ?>"><?php echo $queried_post->post_title; ?></a></h2>
    </div>
  </li>
 <?php endforeach; ?>
</ul>
</div>