個人用ツール

「複数のブログの記事を混ぜて取得してテーマ上でカスタマイズしたい」を編集中

提供: baserCMS公式ガイド

移動: 案内, 検索

警告: ログインしていません。

編集すると、IPアドレスがこのページの編集履歴に記録されます。
この編集を取り消せます。 下記の差分を確認して、本当に取り消していいか検証してください。よろしければ変更を保存して取り消しを完了してください。
最新版 編集中の文章
行10: 行10:
 
$BlogPost = ClassRegistry::init('Blog.BlogPost');
 
$BlogPost = ClassRegistry::init('Blog.BlogPost');
 
$posts = $BlogPost->find('all', array(
 
$posts = $BlogPost->find('all', array(
     'conditions' => array_merge($BlogPost->getConditionAllowPublish()),
+
     'conditions' => array_merge($BlogPost->getConditionAllowPublish()) ),
 
     'order' => array('BlogPost.posts_date DESC'), //公開日順にソート
 
     'order' => array('BlogPost.posts_date DESC'), //公開日順にソート
 
   'limit' => 1000, //取得記事数
 
   'limit' => 1000, //取得記事数
行151: 行151:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
[[Category:サンプル]]
 
+
## 応用:複数ブログをページングするサンプル(トップページ例)
+
 
+
<syntaxhighlight lang="php">
+
<?php
+
// 複数のブログ記事から一覧を取得し、ページングさせるサンプル
+
$BlogPost = ClassRegistry::init('Blog.BlogPost');
+
 
+
// ページング対応
+
$allCount  = 0;
+
$viewCount = 10; // 表示件数
+
$nowPage  = 1;
+
$firstPage = null;
+
$lastPage  = null;
+
$prevPage  = null;
+
$nextPage  = null;
+
$conditions = array_merge($BlogPost->getConditionAllowPublish(), array(
+
'BlogPost.blog_content_id' => array(1,2) // 表示させたいブログコンテンツIDを指定
+
));
+
$allCount =  $BlogPost->find('count', array(
+
'conditions' => $conditions,
+
'cache' => false
+
));
+
if (!empty($this->request->params['named']['page'])) {
+
$nowPage = $this->request->params['named']['page'];
+
}
+
 
+
if ($allCount > $viewCount) {
+
$firstPage = 1;
+
$lastPage = ($allCount % $viewCount == 0) ? intval($allCount / $viewCount) : intval($allCount / $viewCount) + 1;
+
if ($nowPage > $firstPage) {
+
$prevPage = $nowPage -1;
+
} else {
+
$firstPage = null;
+
}
+
if ($nowPage < $lastPage)  {
+
$nextPage = $nowPage +1;
+
} else {
+
$lastPage = null;
+
}
+
}
+
 
+
$posts = $BlogPost->find('all', array(
+
'conditions' => $conditions,
+
'order' => array('BlogPost.posts_date DESC'),
+
'limit' => $viewCount,
+
'offset' => ($nowPage - 1) * $viewCount,
+
'cache' => false
+
));
+
?>
+
 
+
<!-- 一覧を表示 -->
+
 
+
<?php /* トップページの場合の前へ、次へを表示するサンプル */ ?>
+
<?php if ($prevPage) : ?>
+
<?php $this->BcBaser->link("&lt;前", "/index/page:".$prevPage); ?>
+
<?php endif; ?>
+
<?php if ($nextPage): ?>
+
<?php $this->BcBaser->link("次&gt;", "/index/page:".$nextPage); ?>
+
<?php endif; ?>
+
 
+
</syntaxhighlight>
+

baserCMS公式ガイドへのすべての投稿は、他の利用者によって編集、変更、除去される場合があります。 あなたの投稿を、他人が遠慮なく編集するのを望まない場合は、ここには投稿しないでください。
また、投稿するのは、あなたが書いたものか、パブリック ドメインまたはそれに類するフリーな資料からの複製であることを約束してください (詳細は[[Basercms:著作権 ]]を参照)。 著作権保護されている作品を、許諾なしに投稿してはいけません!

Add Categories
Update Categories
  

中止 | 編集の仕方 (新しいウィンドウで開きます)