Yii Framework Forum: How to add "Recent Posts" widget to the blog demo - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to add "Recent Posts" widget to the blog demo Rate Topic: -----

#1 User is offline   mocapapa 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 428
  • Joined: 04-January 09
  • Location:Tokyo, JAPAN

Posted 18 February 2009 - 12:08 AM

It seems to be natural to have a function of "Recent Posts" above "Recent Comments", and here it is.

Posted Image

* Save following code as `blog/protected/components/RecentPosts.php`.
~~~
[php]
<?php

class RecentPosts extends Portlet
{
        public $title='Recent Posts';

        public function getRecentPosts()
        {
                return Post::model()->findRecentPosts();
        }

        protected function renderContent()
        {
                $this->render('recentPosts');
        }
}
~~~ 

* Save following code as `blog/protected/components/views/recentPosts.php`.
~~~
[php]
<ul>
<?php foreach($this->getRecentPosts() as $post): ?>
<li>
<?php echo CHtml::link(CHtml::encode($post->title),array('post/show','id'=>$post->id)); ?>
</li>
<?php endforeach; ?>
</ul>
~~~

* Add following method in the class of `Post` defined by `blog/protected/models/Post.php`.
~~~
[php]
      /**
        * @param integer the maximum number of comments that should be returned
        * @return array the most recently added comments
        */
        public function findRecentPosts($limit=10)
        {
          $criteria=array(
                          'condition'=>'Post.status='.self::STATUS_PUBLISHED,
                          'order'=>'Post.createTime DESC',
                          'limit'=>$limit,
                          );
          return $this->findAll($criteria);
        }
~~~

* Finally, place this widget above the recent comments widget. Modify
`blog/protected/views/layouts/main.php` as follows.
~~~
[php]
+    <?php $this->widget('RecentPosts'); ?>

    <?php $this->widget('RecentComments'); ?>
~~~

Have fun ;)


[Updated: Feb 22, 2009]
Collection of the enhancements:
http://code.google.c...gdemo-enhanced/

Attached File(s)


0

#2 User is offline   mocapapa 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 428
  • Joined: 04-January 09
  • Location:Tokyo, JAPAN

Posted 19 March 2009 - 01:54 AM

I have changed it shown as a following code, though it requires Calendar widget extension.

`blog/protected/components/views/recentPosts.php`:

<ul>
<?php foreach($this->getRecentPosts() as $post): ?>
<li>
<?php echo CHtml::link(CHtml::encode($post->title),array('post/show','id'=>$post->id)); ?>
+ &nbsp;on&nbsp;
+ <?php echo CHTml::link(date('M j', $post->createTime), array('post/PostedOnDate', 'time'=>$post->createTime)); ?>
</li>
<?php endforeach; ?>
</ul>


You can get the latest version from the google code.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users