home-icon
Category content
Last updated: May 30, 2024

Introduction

If you want to create a list of pages within a certain category (a blog overview for example), you can use the getCategoryContent() method. This method gets all the data from the pages within the specified category. Including all sections and elements within the pages.

Usage

So, let's assume we have a category by the name 'blog' and we want to create a nice overview of all blog articles including the title, image and short text.

We also assume that you have created and filled this data (image, title, short text) in de pages of each blog article.

<?php foreach($webigniter->getCategoryContent(handle: 'blog') as $blogArticle):?>
   <div class='title'>
      <?=$blogArticle['Header']['title'];?>
   </div>
    
   <div class='text'>
      <?=$blogArticle['Content']['text'];?>
    </div>
   
   <img src='<?=$blogArticle['Content']['image']['link'];?>' alt='<?=$blogArticle['Content']['image']['alt'];?>'>
<?php endforeach;?>

The handle can be found in the categories overview in the CMS, when logged in as an admin.

Let's brake this code down into pieces...

We start with a foreach() loop through all blog articles and store each blogarticle in the variable $blogArticle.
Within the foreach loop you see we access $blogArticle['Header']['title']. This means we are going to access the element 'title' from the section 'Header'. The same applies to $blogArticle['Content']['text'].
You can see $blogArticle['Content']['image']['link'] is a little different, that's because of the way the media element works. This particular piece gets the link (URL) of the media file by the name 'image' in the section 'Content'.

Note that it is important that ALL pages in the category with the handle 'blog' have the sections you are using with getCategoryContent(), or it will result in errors. You can set a preset of sections on each category, so when a page withing that category is created, it has this preset of sections by default. So in our example we will add the sections 'Header' and 'Content' to make sure we always have this data.

Table of Contents
  1. Introduction
  2. Usage