当前位置: arrow 精彩案例 arrow 开源拾遗 arrow WordPress里get_posts与WP_Query的一点使用

WordPress里get_posts与WP_Query的一点使用

2008-12-12

 WordPress 文章下面调用随机帖子的方法:

a:

<?php
$rand_posts = get_posts('&showposts=9&orderby=rand');
foreach( $rand_posts as $post ) :
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>

b:

 
<?php
$rand_posts = new WP_Query("&showposts=9&orderby=rand");
if ($rand_posts->have_posts()) : while ($rand_posts->have_posts()) : $rand_posts->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile;endif; ?>

a直接使用get_posts很方便,如果后面有使用loop的的话,应该会有"goto"的问题, b使用WP_Query前后毕合,在复杂情况下应用比较自如.