改变wordpress首页文章显示的数量虽然在后台可以设置但是了解一下控制显示数量的代码并不是什么坏事。query_posts()就可以轻松实现
,但不是首选或最有效,下面我用query_posts()讲解几个简单实用的例子,一切以举例说明。
1,例如想改变wordpress首页显示文章数量。
找到index.php文件查询代码:
<?php while ( have_posts() ) : the_post(); ?>
修改为:
<?php query_posts( ‘posts_per_page=5’);while ( have_posts() ) : the_post(); ?>
这样首页显示文章数量就是5篇
2,改变wordpress以列表方式显示最新文章。
我们用图文说明,将你先显示的地方插入以下代码即可:
[code]<?php
// The Query
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post();
echo ‘<li>’;
the_title();
echo ‘</li>’;
endwhile;
// Reset Query
wp_reset_query();
?>[/code]
如图:

3,改变wordpress发布文章先后顺序
或许你希望让最早发布的文章永远置顶新发布的文章紧靠上篇文章的后面,也就是说文章排列顺序是以降序:早到晚依次排列的。
找到index.php文件查询代码:
<?php while ( have_posts() ) : the_post(); ?>
修改为:
<?php global $query_string;query_posts( $query_string . ‘&order=ASC’ ); while ( have_posts() ) : the_post(); ?>
4,只显示wordpress某分类目录在首页
找到index.php文件查询代码:
<?php while ( have_posts() ) : the_post(); ?>
修改为:
<?php query_posts( ‘cat=5&year=2012’ );while ( have_posts() ) : the_post(); ?>
其中的
query_posts( ‘cat=5&year=2012’ );
的意思是:只显示这个文章类ID=5并且年份是2012在首页其他的都不显示。
5,wordpress某文章类当月发布文章显示在首页
[code]if ( is_home() ) {
query_posts( $query_string . ‘&cat=13&monthnum=’ . date( ‘n’, current_time( ‘timestamp’ ) ) );
}[/code]
意思是文章类ID=13的当月发布的文章显示在首页
6,让wordpress某分类目录文章不显示在首页
找到index.php文件查询代码:
<?php while ( have_posts() ) : the_post(); ?>
修改为:
<?php query_posts($query_string .’&cat=-17′);while ( have_posts() ) : the_post(); ?>
意思就是让ID=17的分类目录文章不显示在首页我们也可以写多个值:
<?php if ( is_home() ) {query_posts( ‘cat=-1,-2,-3’ );};while ( have_posts() ) : the_post(); ?>
7,wordpress显示特定文章
<?php while ( have_posts() ) : the_post(); ?>
修改为:
<?php query_posts( ‘p=5’ );while ( have_posts() ) : the_post(); ?>
8,wordpress显示特定附件
<?php while ( have_posts() ) : the_post(); ?>
修改为:
<?php query_posts( ‘attachment_id=5’ );while ( have_posts() ) : the_post(); ?>
还有很多就不写了这些算是比较常用的。