我最近编写了一个特色部分,该部分实际上将拉取数组中指定的帖子。这是代码
<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>
现在,此代码在home
,archive
和任何具有post
循环的页面上完美运行,并且它显示了4
假定的特色帖子,但是问题它没有显示在任何其他没有post
循环的页面上,例如single.php
或contact 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>