介绍一种方法:get_posts
<?php $posts = get_posts( "category=1&numberposts=10" ); ?>
<?php if( $posts ) : ?>
<?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
<li>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>
<?php endif; ?>
- category=1&numberposts=10:其中的1是指调用分类ID为1的文章,10是指调用该分类下最新的10篇文章
值得注意的是这一句话,当时写的时候写错了,纠错了很久。
™
<?php $posts = get_posts( "category=1&numberposts=10" ); ?>
这一句中的1为分类ID为1的文章,但是带调用的时候不能直接调用他的ID:<?php $posts = get_posts( "1&10" ); ?>
这个样子是错误的。参数中的category或者numberposts是必不可少的,不能只写他的ID数字。
当需要调用分下的全部文章的时候,把numberposts=-1
即可。如下:
<?php $posts = get_posts( "category=1&numberposts=-1" ); ?>