如何循环输出文章列表

一下查出多少文章出来

query_posts( 'posts_per_page=4' );

  1. 判断文章是否存在
    have_posts();
  2. 获取下一篇文章信息并且把信息存入全局变量$post
    the_post();
  3. 获取文章的标题
    the_title();
  4. 获取文章的链接
    the_permalink();
  5. 获取文章的内容
    the_content();

综合应用

<?php
             query_posts( 'posts_per_page=4' );
             if(have_posts()){
                 while (have_posts()){
                 the_post();
             ?>
             <div class="col-sm-4 col-md-3 col-mm-6 index_news">
                        <span><? the_time('Y-m-d'); ?></span>
                        <h3>
                            <a href="<?php the_permalink();?>" title="<? the_title(); ?>"><? echo mb_strimwidth(strip_tags(apply_filters('the_title', $post->post_title)), 0, 30,"..."); ?></a></h3>
                        <p>
                            <?php if (has_excerpt()) {
                                echo $description = get_the_excerpt(); //文章编辑中的摘要
                            }else {
                                echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 100,"...");
//文章编辑中若无摘要,自动截取文章内容字数做为摘要,0表示开始的位置,170表示结束的位置
                            } ?>
                        </p>
                        <a href="<? the_permalink(); ?>" class="new_btn">详细&gt;&gt;</a></div>
             <?php
                 }
             }else{
                echo '暂无新闻,感谢您的关注!';
             }?>

本文由 admin 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

楼主残忍的关闭了评论