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.

Category page

The blog category page contains a list of blog posts belonging to a specific category. To identify a category the blog module uses the category URL Name, which you can specify on the Create/Edit Category page of the Administration Area. A category URL Name is passed to the category page in its URL. For example, if you created a category page with the /blog/category URL, the URL of the News category page would be /blog/category/news.

Create a new page on the CMS/Pages page. Assign the page a suitable URL (for example /blog/category) and select the blog:category action on the Actions tab. The blog:category action loads a category by a URL Name specified in the page URL, from the database, and creates two PHP variables which you can use in the page code: $category and $posts.

The $category variable contains a reference to the Blog_Category class instance, representing a requested category. The $posts 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 $category variable can has the NULL value if a specified category was not found. You should check the variable value before displaying any category details or the category post list. The $post variable will not be created if the category was not found.

The code below outputs a category name and a list of its posts. The code is similar to the code described in the Archive Page article, except that now we display a list of a specific category posts.

<? if ($category): ?>
  <h2><?= h($category->name) ?></h2>

  <? $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>
<? else: ?>
  <h2>Category not found</h2>
<? endif ?>

Paginating the category post list

Paginating the category posts is very similar to the process described in the Archive Page article. Before calling the $posts->find_all() method we need to call the $posts->paginate() method and pass a page index and a number of posts per page to it. Please note that the first parameter in the request_param() method call is 1. The first parameter on the category page is a category URL name, so the page index will be passed via the second parameter (with index 1). Also, the base URL parameter which we pass to the pagination partial now should include the current category URL name. Below is a final code of the category page with pagination. In the code example we assume that the blog category page has the "/blog/category" URL.

<? if ($category): ?>
  <h2><?= h($category->name) ?></h2>

  <?
    $pagination = $posts->paginate($this->request_param(1, 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/category/'.$category->url_name)) ?>
<? else: ?>
  <h2>Category not found</h2>
<? endif ?>

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