Blog sayfalarında yazıların altında önceki yazı ve sonraki yazı(ları) göstermek isteyebiliriz. Bunun için ilgili Model dosyasında(örn: backend/models/Posts
) aşağıdaki fonksiyonları tanımlamamız gerekir.
public static function getNextId($currentId)
{
$next=\backend\models\Posts::find()
->where(['>', 'postID', $currentId])
->one();
return ['postTitleSeo'=>$next->postTitleSeo,'postTitle'=>$next->postTitle];
}
public static function getPrevId($currentId)
{
$prev=\backend\models\Posts::find()
->where(['<', 'postID', $currentId])
->orderBy('postID desc')
->one();
return ['postTitleSeo'=>$prev->postTitleSeo,'postTitle'=>$prev->postTitle];
}
Daha sonra ilgili view
sayfasında aşağıdaki gibi kullanabiliriz.
<div class="row">
<div class="col-md-6">
<div class="post-pagination pagination-prev">
<span>Önceki Yazı</span>
<a href="<?=Yii::$app->urlManager->createUrl(['site/postdetails','postTitleSeo'=>$post->getPrevId($post->postID)['postTitleSeo']])?>">
<h4><?=$post->getPrevId($post->postID)['postTitle']?></h4>
</a>
</div>
</div>
<div class="col-md-6">
<div class="post-pagination pagination-next">
<span>Sonraki Yazı</span>
<a href="<?=Yii::$app->urlManager->createUrl(['site/postdetails','postTitleSeo'=>$post->getNextId($post->postID)['postTitleSeo']])?>">
<h4><?=$post->getNextId($post->postID)['postTitle']?></h4>
</a>
</div>
</div>
</div>