【发布时间】:2011-06-12 13:49:39
【问题描述】:
我有一个模板文件 (trendingPosts.php) 用于显示 2 个带有“趋势”标签的最新帖子。在显示这 2 个帖子的 while 循环中,我将它们的 ID 放在一个数组中,以便稍后将它们从主 wordpress 循环中排除:
<div id="trendingWrap" class="clearfix">
<?php
$trending = new WP_Query();
$trending->query("showposts=2&tag=trending");
while($trending->have_posts()) : $trending->the_post();
$wp_query->in_the_loop = true;
$currentTrending[] = $post->ID;
?>
<div class="trendingStory">
<h2 class="trendingTitle"><a href="<?php the_permalink(); ?>" alt="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
</div><!-- end trendingStory -->
<?php endwhile; ?>
</div><!-- end trendingWrap -->
问题是我有一个 index.php,其中我通过 get_template_part( 'loop', 'index' ); 包含 loop.php 并且我无法获得 $currentTrending[] 数组我在 trendingPosts.php 中制作的。 我需要在我的 loop.php
另外,在我的loop.php中,我排除了以下2个帖子。
if(have_posts()): while(have_posts()) : the_post();
if( $post->ID == $currentTrending[0] || $post->ID == $currentTrending[1] ) continue;
这是排除帖子的正确方法吗?如果有人有更好的方法来做这件事。请告诉我。 当然,在我设法在 loop.php 中获取该数组之前,没有任何效果,所以这是主要问题。
谢谢!感谢所有帮助。
【问题讨论】:
-
非常感谢您的回复,但下面的方法对我有效,我的代码几乎没有变化。
-
你应该把这个评论放在另一个答案的下面;)投票总是一种很好的表达感谢的方式(小提示)。
标签: php wordpress-theming wordpress