WordPress调用指定分类的文章代码如下:
1 2 3 4 5 |
<ul> <?php query_posts('cat=15&posts_per_page=10'); while(have_posts()): the_post(); ?> <li><a href="<?php the_permalink();?>" title="<?php the_title(); ?>"><?php the_title();?></a></li> <?php endwhile; wp_reset_query(); ?> </ul> |
其中cat表示调用的分类ID,posts_per_page是需要显示的文章数量,自己来根据需要进行相应的调整即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<ul> <?php $args=array( 'cat' => 1, // 分类ID 'posts_per_page' => 10, // 显示篇数 ); query_posts($args); if(have_posts()) : while (have_posts()) : the_post(); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> //标题 <span> <?php if (has_excerpt()) { echo $description = get_the_excerpt(); //文章编辑中的摘要 }else { echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 170,"……"); //文章编辑中若无摘要,自定截取文章内容字数做为摘要 } ?> </span> </li> <?php endwhile; endif; wp_reset_query(); ?> </ul> |
如果想从第二篇开始调用可以加一个参数&offset=1,第三篇开始调用就用&offset=2,以此类推。
注意:此方法可能会有冲突,请使用wp_query函数