複数のブログの記事を混ぜて取得してテーマ上でカスタマイズしたい
提供: baserCMS公式ガイド
2016年2月15日 (月) 15:28時点における183.76.75.203 (トーク)による版
現状、該当するヘルパー関数がないため、cakePHPの記述が必要となります。 以下の記述をテーマファイル上に書けば、全ブログの公開状態の記事が取得できます。 どのテーマファイル上でも利用可能です。
<?php /* 全ブログで公開状態の記事を取得 */ $BlogPost = ClassRegistry::init('Blog.BlogPost'); $posts = $BlogPost->find('all', array( 'conditions' => array_merge($BlogPost->getConditionAllowPublish()), 'order' => array('BlogPost.posts_date DESC'), //公開日順にソート 'limit' => 1000, //取得記事数 'cache' => false //キャッシュはオフに )); // 記事の確認 // var_dump($posts); ?>
'cache' => false は指定しておかないと、サーバー上にキャッシュファイルが延々と溜まります。(数GBになるケースも) 負荷をかけてしまうので必ず入れておきましょう。
応用:必要なブログのみを指定する
<?php /* ブログIDで取得したいブログを指定 */ $BlogPost = ClassRegistry::init('Blog.BlogPost'); $posts = $BlogPost->find('all', array( 'conditions' => array_merge($BlogPost->getConditionAllowPublish(), array( // NEWS、ブログ 'BlogPost.blog_content_id' => array(1,3,4) //取得したいblogIDをカンマ区切りで指定 )), 'order' => array('BlogPost.posts_date DESC'), 'limit' => 1000, 'cache' => false )); ?>
応用:指定したブログでスライドショー (Slider Proの場合)
複数ブログ(blog ID= 1,2,3)から最新記事5件をSlider Pro - Elegant and Professional Slidersでスライドショーさせるサンプルです。
<?php //スライド表示件数 $sliderNum = 5; /* 指定ブログの記事を取得 */ $BlogPost = ClassRegistry::init('Blog.BlogPost'); $posts = $BlogPost->find('all', array( 'conditions' => array_merge($BlogPost->getConditionAllowPublish(), array( 'BlogPost.blog_content_id' => array(1,2,3) )), 'order' => array('BlogPost.posts_date DESC'), 'limit' => $sliderNum, 'cache' => false )); ?> <div id="area-main-image" class="slider-pro"> <div class="sp-slides"> <?php if ($posts): ?> <?php foreach ($posts as $key => $post): ?> <?php //指定スライド数でループ終了 if( $key == $sliderNum ) break; $blogName = $post["BlogContent"]["name"]; $baseCurrentUrl = "/".$blogName . '/archives/'; $baseCurrentImgUrl = "/files/blog/".$blogName . "/blog_posts/"; $postLink = $this->BcBaser->getUrl($baseCurrentUrl.$post['BlogPost']['no'],true); if($post["BlogPost"]["eye_catch"]){ // アイキャッチ画像のパス $eyeCatch = $baseCurrentImgUrl . $post["BlogPost"]["eye_catch"]; $path = realpath( "."); //フルパス取得 $eyeCatch_m = str_replace(".jpg","__thumb.jpg", $eyeCatch); $eyeCatch_s = str_replace(".jpg","__mobile_thumb.jpg", $eyeCatch); // もしサムネイルが生成されていればそちらを利用する // アイキャッチ画像の大サイズを利用する if(file_exists( $path.$eyeCatch_m )) { $eyeCatch = $eyeCatch_m; } } else { $eyeCatch = "/_shared/img/no-photo.png"; } $id = 'post-'.$post['BlogPost']['no']; ?> <div class="sp-slide"> <img class="sp-image" src="<?php echo $eyeCatch; ?>" data-src="<?php echo $eyeCatch; ?>" data-small="<?php echo $eyeCatch_s; ?>" data-medium="<?php echo $eyeCatch_s; ?>"> <div class="sp-layer sp-black sp-padding" data-position="bottomLeft" data-horizontal="0" data-vertical="0" data-width="100%" data-height="100%"> <div class="wrap" id="slide-<?php echo $id; ?>"> <a href="<?php echo $postLink; ?>"> <time datetime="<?php $this->Blog->postDate($post, 'Y.m.d') ?>"><?php $this->Blog->postDate($post, 'Y.m.d') ?></time> <h2 class="slide-title"><span><?php $title = $this->Blog->getPostTitle($post,false); // カットする指定文字数 $num = 35; if(mb_strlen($title) >= $num) { echo mb_substr($title, 0,$num)."…"; } else { echo $title; } ?></span></h2> <p class="slide-description"> <?php // $post['BlogPost']['content'] //概要 // $post['BlogPost']['detail'] //本文 // カットする指定文字数 $num = 84; //概要がある場合 if($post['BlogPost']['content']) { $str = strip_tags($post['BlogPost']['content']); }else { //概要がない場合:本文を文字数指定で出力 $str = strip_tags($post['BlogPost']['detail']); } if(mb_strlen($str) >= $num) { echo mb_substr($str, 0,$num)."…"; } else { echo $str; } ?> </p> </a> <div class="box-botn"> <a href="<?php echo $postLink; ?>" class="btn btn-square">この記事を読む</a> </div> </div><!-- /.wrap --> </div><!-- /.sp-layer --> </div><!-- /.sp-slide --> <?php endforeach; ?> <?php else: ?> <p class="no-data">記事がありません</p> <?php endif ?> </div><!-- /.sp-slides --> </div> <!-- /#area-main-image -->
<?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("<前", "/index/page:".$prevPage); ?> <?php endif; ?> <?php if ($nextPage): ?> <?php $this->BcBaser->link("次>", "/index/page:".$nextPage); ?> <?php endif; ?>