Page 1 of 1

How to show list of childs sorted by custom data

Posted: Fri Feb 17, 2023 12:26 pm
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? :)

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

Posted: Fri Feb 17, 2023 4:00 pm
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

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

Posted: Fri Feb 17, 2023 5:22 pm
by nesworld
Yeah that might be it... and I did install the LISE module, but was unable to figure it out.

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

Posted: Fri Jul 14, 2023 11:31 am
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.