WordPress get_posts problem; adding category argument seemingly nullifies numberposts
I have two different get_posts functions as follows:
<!-- Begin Left Middle -->
<div id="leftmiddle">
<h1><a href="/category/recent-cases">Recent Cases</a></h1>
<?php
global $post;
$postslist = get_posts('category=5&numberposts=2');
foreach ($postslist as $post) :
setup_postdata($post);
?>
<div id="latest_post">
<span class="theme date"><?php the_date(); ?></span>
<br />
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php the_excerpt(); ?>
</div>
<?php endforeach; ?>
</div>
<!-- End Left Middle -->
<!-- Begin Right Middle -->
<div id="rightmiddle">
<h1><a href="/blog">Latest Posts</a></h1>
<?php
$postslist = get_posts('numberposts=2&order=DESC&orderby=date&category=-5');
foreach ($postslist as $post) :
setup_postdata($post);
?>
<div id="latest_post">
<span class="theme date"><?php the_date(); ?></span>
<br />
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php the_excerpt(); ?>
</div>
开发者_高级运维 <?php endforeach; ?>
</div>
<!-- End Right Middle -->
Now in rightmiddle it does everything as expected; lists only the two most recent posts that aren't in category 5. However, in leftmiddle it lists all the posts in category 5. If I remove the category argument in leftmiddle or I change it to a negating param like in rightmiddle, numberposts works as expected. Why is that?
Try using a two different variable names to hold the return values from your calls to get_posts()
. Right now you are using the same variable name $postslist
. They might be interfering, which is why the last call in rightmiddle works.
You are also declaring global $post
in leftmiddle but not rightmiddle.
精彩评论