Silverstripe Version: 4
Question: I need help with displaying the results of the first category when a user visits the page first time.
I have a page where it lists online courses under each course category. I need help with displaying the results of the first category when a user visits the page first time. For ex: I want to show the courses in under Microsoft category first whenever a user visits the page. Currently the Microsoft category tab is active on the first load but all the courses under each category are shown. However if I click any category tab, it displays the courses relevent to each category correctly. Theres no issue in that. I only want to display the Courses under first category which is ‘Microsoft’ when a user visits the page first time.
Working photo when a tab is clicked:
How I want it to view when the page load first:
Thanks.
// Template.ss
<div class="filter-menu -mx-4 py-4">
<% if $Categories %>
<ul class="flex flex-wrap px-2">
<% loop $Categories %>
<li class="filter-menu__item mx-2 mb-2 <% if $Category =='Microsoft' %> active <% end_if %>" active data-filter=".$LowerCaseName">
<a class="button">$Category</a>
</li>
<% end_loop %>
</ul>
<% end_if %>
</div>
<div class="-mx-4">
<% if $Courses %>
<ul class="filter-items flex flex-wrap">
<% loop $courses %>
<li class="item $Category.LowerCaseName p-4 w-full md:w-1/2 lg:w-1/3">
<a href="$Top.CoursesPageURLSegment/course/$URLSegment"
class="bottom-border w-full h-full py-4 block flex items-center justify-between">
<div class="flex flex-wrap">
<% if $Icon.Extension == 'svg' %>
<img class="w-1/6" src="$Icon.URL">
<% else %>
<img class="w-1/6" src="$Icon.Fill(100, 100).URL">
<% end_if %>
<span class="mt-5 pl-3">$Title</span>
</div>
<span class="w-6">
<% include Icons Icon=RightArrow %>
</span>
</a>
</li>
<% end_loop %>
</ul>
<% else %>
<p>No items found</p>
<% end_if %>
</div>
//------ Elemental PHP Code -------//
public function getCategories()
{
$categories = CourseCategory::get();
if ($categories && $categories->exists()) {
return $categories;
}
}
public function getCourses() //gets all courses
{
$courses = Course::get();
if ($courses && $courses->exists()) {
return $courses;
}
}
public function getCoursesPageURLSegment()
{
$page = \Page::get()->filter('Title', 'Courses')->first();
if ($page && $page->exists()) {
return $page->URLSegment;
}
}
//------ CourseCategory Model -------//
private static $db = [
'Category' => 'Varchar'
];
private static $summary_fields = [
'Category'
];
private static $table_name = 'CourseCategory';
public function getLowerCaseName()
{
return strtolower($this->Category);
}