如何限制在CakePHP中创建的“最近博客”的数量?

时间:2017-03-17 07:43:51

标签: php cakephp cakephp-3.0

我正在尝试创建一个博客,并且在每个博客中都会显示最近在页面底部创建的博客。有没有办法可以将这个数字限制在最近的4个博客中?因为当我生成它时,所有已创建的博客都显示在“最近的博客”区域。

 <div class="container" id="newsextra">
    <h4>MORE NEWS</h4>
    <div class="row">

        <?php 
        if(!empty($error)){
        echo $error;
    }
    if (!empty($blogsinfos)) {  
    foreach ($blogsinfos as $blogs): ?>

    <div class="col-md-3">
        <a href="/news-single/<?=  h($blogs->id)?>">
        <img src="<?=  h($blogs->mainimg)?>" class="img-responsive">
        <h5><?=  h($blogs->title)?></h5>
        <h6><?=  h($blogs->created)?></h6>
        </a>
    </div>

      <?php 
    endforeach; 
        } 
    ?>
    </div>
</div>

2 个答案:

答案 0 :(得分:0)

您应该从控制器中限制生成的帖子数量。谢谢@ Rik.esh的答案。

 $this->loadModel('Blogs');
 $opts1['conditions'] =  array('Blogs.status' => 1);
 $opts1['limit'] = 4;
 $opts1['order'] = array('Blogs.created' => 'desc');
 $blogsinfos = $this->Blogs->find('all',$opts1);
 $this->set('blogsinfos', $blogsinfos);
 $this->set('_serialize', ['blogsinfos']);

 foreach ($blogsinfos as $blogs) {
    $proid = $blogs['id'];
 }

答案 1 :(得分:-1)

试试这个

$opts1['order'] = 'Blogs.created desc';