How to show list of childs sorted by custom data

For discussion and questions related to CMS Specific templates and stylesheets (CSS), and themes. or layout issues. This is not a place for generic "I don't know CSS issues"
Post Reply
nesworld
New Member
New Member
Posts: 2
Joined: Fri Feb 17, 2023 12:18 pm

How to show list of childs sorted by custom data

Post by nesworld »

Hi

So I have a list of childs in my "articles" section and while it's super easy to just show a list of these I would like to sort these. My site is about video games, so I would like to sort them into lists for each videogame system, like this old "non CMS" page

https://nesworld.com/content.php?data=unreleased

I do have the system as a custom entry when I create content, but is there any why to use these customs fields to show a list?

Hope it makes sense? :)
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1625
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: How to show list of childs sorted by custom data

Post by DIGI3 »

It's not totally clear but I think you're referring to sorting content pages by a keyword. If you're not too far in, I'd suggest switching to using LISE for any information that needs to be listed and sorted, it's exactly what it's designed for. While it could be done with content pages and Navigator it's going to be less efficient.
http://dev.cmsmadesimple.org/projects/lise
Not getting the answer you need? CMSMS support options
nesworld
New Member
New Member
Posts: 2
Joined: Fri Feb 17, 2023 12:18 pm

Re: How to show list of childs sorted by custom data

Post by nesworld »

Yeah that might be it... and I did install the LISE module, but was unable to figure it out.
User avatar
nehakakar
New Member
New Member
Posts: 1
Joined: Tue Jul 04, 2023 7:25 am
Contact:

Re: How to show list of childs sorted by custom data

Post by nehakakar »

You can use sort the list of child pages based on the custom system field.
Try this..

Code: Select all

{php}
$children = cmsms()->GetHierarchyOperations()->GetChildren($content->Id());

$groups = [];
foreach ($children as $child) {
    $system = $child->custom_fields['system']; // Replace 'system' with your custom field name
    $groups[$system][] = $child;
}

// Sort the groups based on the system field value
ksort($groups);

foreach ($groups as $system => $group) {
    // Display the system name
    echo "<h2>$system</h2>";

    // Sort the child pages within each group if needed
    // Example: Sorting by child page name
    usort($group, function($a, $b) {
        return strcmp($a->Name(), $b->Name());
    });

    // Display the child pages within the group
    foreach ($group as $child) {
        echo $child->Name(); // Example: Displaying the child page name
    }
}
{/php}
The custom field 'system' is accessed using $child->custom_fields['system']. Replace 'system' with the name of your custom field. You can modify the sorting logic within the usort() function to sort the child pages within each group based on your requirements by name, date, etc.
Post Reply

Return to “Layout and Design (CSS & HTML)”