This is the documentation for LemonStand V1, which has been discontinued. You can learn more and upgrade your store here.

LemonStand Version 1 Has Been Discontinued

This documentation is for LemonStand Version 1. LemonStand is now offered as a cloud-based eCommerce platform.
You can try the new LemonStand and learn about upgrading here.

Implementing the blog archive page

On the blog archive page you can display blog published posts from all categories. To create the archive page, create a new page on the CMS/Pages page of the Administration Area. You can assign any URL for this page, for example "/blog". On the Actions tab select the blog:archive action.

The blog:archive action creates the $posts PHP variable, which you can use in the page code. The variable contains a instance of the Blog_Post class. To obtain a list of posts, you need to call the find_all() method of the $posts variable. The find_all() method returns a collection of blog posts. Each element in the collection is an instance of the Blog_Post class. The following code outputs a list of posts.

<? $post_list = $posts->find_all() ?> 
<ul>
  <? foreach ($post_list as $post): ?>
  <li>
    <h3><?= h($post->title) ?></h3>
    <p>
      Published by <?= h($post->created_user_name) ?>
      on <?= $post->published_date->format('%F') ?>
      Comment(s): <?= $post->approved_comment_num ?>
    </p>
    <p><?= h($post->description) ?></p>
  </li>
<? endforeach ?>
</ul>

For each post in the collection the code outputs its title, author name, publication date, a number of approved comments and a description. You can find a full list of available post object fields on this page.

Adding links to post pages

In the next article we will explain how you can implement the blog post page. Assuming that your blog post page URL is /blog/post, you can add links to the blog post list with the following code:

<p><?= h($post->description) ?></p>
<p><a href="/blog/post/<?= $post->url_title ?>">Read more...</a></p>

Below is an example of the blog archive page view:

Paginating the post list

To paginate the post list, you need to call the paginate() method of the $posts variable. This method accepts 2 parameters - a page index, and a number of posts to output on a single page. Pagination uses the page URL to pass a page index when you navigate between pages. If your blog archive page URL was "/blog", the URL of the second page would be /blog/2. You can load URL segments using the request_param() method of the controller object (represented with $this variable). To load the page index from the page URL, we need to load the second URL segment value: $this->request_param(0, 1). The pagination works with zero-based page indexes, so we need to subtract 1 from the result. The final code for paginating the post result looks as follows. Please note that the find_all() method is called after the paginate() method call.

<?
  $pagination = $posts->paginate($this->request_param(0, 1)-1, 2);
  $post_list = $posts->find_all(); 
?>

To output the page links, you can use the pagination partial, described in the Displaying links to other pages section of the Displaying a list of products article. In the default store implementation there is partial "pagination", which outputs page links. You can render it below the post list:

<? $this->render_partial('pagination', array('pagination'=>$pagination, 'base_url'=>'/blog')) ?>

Please note that the base_url parameter in the code example refers to the "/blog" URL. You should use an actual URL of your blog archive page.

Below is a final code of the blog archive page with pagination.

<?
  $pagination = $posts->paginate($this->request_param(0, 1)-1, 2);
  $post_list = $posts->find_all();  
?>
<ul>
  <? foreach ($post_list as $post): ?>
    <li>
      <h3><?= h($post->title) ?></h3>
      <p>
        Published by <?= h($post->created_user_name) ?>
        on <?= $post->published_date->format('%F') ?>
        Comment(s): <?= $post->approved_comment_num ?>
      </p>
      <p><?= h($post->description) ?></p>
      <p><a href="/blog/post/<?= $post->url_title ?>">Read more...</a></p>
    </li>
  <? endforeach ?>
</ul>

<? $this->render_partial('pagination', array('pagination'=>$pagination, 'base_url'=>'/blog')) ?>

Next: Displaying a blog category list
Previous: Blog module
Return to Blog module