创建5个最新文章的列表

时间:2014-07-10 15:38:04

标签: list article

我有一个定制的PHP / MySql网站,在主页上有一个最新的高级文章列表(称为Summaries)。问题是,在发布内容几个月后,主页变得比它应该更长。如何将帖子数量限制在5个,比如说最新的5个帖子?这是生成不断增长的文章列表的代码,包括缩略图,Contine Reading等。谢谢!

                  <div id="summaries">

                 <?php if($summaries){ 
                   $count=0;
                   foreach($summaries as $article) {
                   $date_string = date('F j, Y',strtotime($article['art_date_added']));
                   $author_name = $article['adm_full_name'];
                   if($article['art_excerpt'])
                   {
                     $excerpt = substr(strip_tags($article['art_excerpt']),0,340).'[...]' ; 
                   }
                   else
                   {
                     $excerpt = substr(strip_tags($article['art_content']),0,340).'[...]' ;   
                   }
                    if($count % 2 == 0)
                      {
                         $even = TRUE;  
                      }
                      else
                      {
                         $even=FALSE;  
                      }
                    $count++;
                 ?>
                 <?php if($even) { ?>                    
                 <article class="article">
                    <a href="<?=$article['permalink']; ?>" class="listThumbnail">
                    <?php if(!$article['art_image']) { ?>
                    <img src="<?php echo base_url(); ?>site_images/website_images/default.png" width="120px" class=""/></a>
                    <?php }else { ?>
                    <img src="<?php echo base_url(); ?>uploads/<?=$article['art_image']; ?>" width="120px" class=""/></a>
                    <?php } ?>
                    <header class="col-lg-9">
                       <a href="<?=$article['permalink']; ?>"><h2><?=$article['art_title']; ?></h2></a>
                       <div class="meta">
                        Posted by <b><?php echo $article['adm_full_name']; ?></b> on <?=$date_string; ?></em> 
                        | <b><?=$article['comments_total']; ?> comments</b></div>
                       <p>
                         <?=$excerpt; ?>
                         <a href="<?=$article['permalink']; ?>" class="cr">Continue Reading </a>
                       </p>
                    </header>
                 <div class="clearfix"></div>   
                 </article>

                 <?php } ?>

                 <?php if(!$even) { ?>
                 <article class="article">
                    <a href="<?=$article['permalink']; ?>" class="listThumbnailR">
                    <?php if(!$article['art_image']) { ?>
                    <img src="<?php echo base_url(); ?>site_images/website_images/default.png" width="120px" class=""/></a>
                    <?php }else { ?>
                    <img src="<?php echo base_url(); ?>uploads/<?=$article['art_image']; ?>" width="120px" class=""/></a>
                    <?php } ?>
                    <header class="col-lg-9">
                       <a href="<?=$article['permalink']; ?>"><h2><?=$article['art_title']; ?></h2></a>
                       <div class="meta">
                        Posted by <b><?php echo $article['adm_full_name']; ?></b> on <?=$date_string; ?></em> 
                        | <b><?=$article['comments_total']; ?> comments</b></div>
                       <p>
                         <?=$excerpt; ?>
                         <a href="<?=$article['permalink']; ?>" class="cr">Continue Reading </a>
                       </p>
                    </header>
                 <div class="clearfix"></div>   
                 </article>

                 <?php } ?>
                 <?php }} ?>
              </div>

2 个答案:

答案 0 :(得分:0)

最好的方法是限制从服务器发送到客户端的数据。我想你正在使用查询来提取所有记录(或超过5的记录)。只需在查询或ORM查询代码中使用LIMIT子句即可返回最新的5个项目。

答案 1 :(得分:0)

好的,所以这笔交易是设置一个计数器,并将其限制为给定的Summaries文本类别中的5个最新文本。这是完整的代码,按预期工作

                 <?php if($summaries){ 
                   $count=0;

                   foreach($summaries as $article) {
                   if($count>=5){ break; } // Choose number of summaries shown in homepage
                   $date_string = date('F j, Y',strtotime($article['art_date_added']));
                   $author_name = $article['adm_full_name'];
                   if($article['art_excerpt'])
                   {
                     $excerpt = substr(strip_tags($article['art_excerpt']),0,340).'[...]' ; 
                   }
                   else
                   {
                     $excerpt = substr(strip_tags($article['art_content']),0,340).'[...]' ;   
                   }
                    if($count % 2 == 0)
                      {
                         $even = TRUE;  
                      }
                      else
                      {
                         $even=FALSE;  
                      }
                    $count++;
                 ?>
                 <?php if($even) { ?>                    
                 <article class="article">
                    <a href="<?=$article['permalink']; ?>" class="listThumbnail">
                    <?php if(!$article['art_image']) { ?>
                    <img src="<?php echo base_url(); ?>site_images/website_images/default.png" width="120px" class=""/></a>
                    <?php }else { ?>
                    <img src="<?php echo base_url(); ?>uploads/<?=$article['art_image']; ?>" width="120px" class=""/></a>
                    <?php } ?>
                    <header class="col-lg-9">
                       <a href="<?=$article['permalink']; ?>"><h2><?=$article['art_title']; ?></h2></a>
                       <div class="meta">
                        Posted by <b><?php echo $article['adm_full_name']; ?></b> on <?=$date_string; ?></em> 
                        | <b><?=$article['comments_total']; ?> comments</b></div>
                       <p>
                         <?=$excerpt; ?>
                         <a href="<?=$article['permalink']; ?>" class="cr">Continue Reading </a>
                       </p>
                    </header>
                 <div class="clearfix"></div>   
                 </article>

                 <?php } ?>

                 <?php if(!$even) { ?>
                 <article class="article">
                    <a href="<?=$article['permalink']; ?>" class="listThumbnailR">
                    <?php if(!$article['art_image']) { ?>
                    <img src="<?php echo base_url(); ?>site_images/website_images/default.png" width="120px" class=""/></a>
                    <?php }else { ?>
                    <img src="<?php echo base_url(); ?>uploads/<?=$article['art_image']; ?>" width="120px" class=""/></a>
                    <?php } ?>
                    <header class="col-lg-9">
                       <a href="<?=$article['permalink']; ?>"><h2><?=$article['art_title']; ?></h2></a>
                       <div class="meta">
                        Posted by <b><?php echo $article['adm_full_name']; ?></b> on <?=$date_string; ?></em> 
                        | <b><?=$article['comments_total']; ?> comments</b></div>
                       <p>
                         <?=$excerpt; ?>
                         <a href="<?=$article['permalink']; ?>" class="cr">Continue Reading </a>
                       </p>
                    </header>
                 <div class="clearfix"></div>   
                 </article>

                 <?php } ?>
                 <?php }} ?>
              </div>