1

In the code below, it possible to sort the results alphabetically - and if so what changes do I need?

<?php 
$templates = get_page_templates();
foreach ( $templates as $template_name => $template_filename ) {
echo "$template_name ($template_filename)<br />";
}
?>

If that code can't be adapted, what's an alternative query?

1

You can use ksort() function to sort the array of $templates = get_page_templates();. So the full code will be-

<?php
$templates = get_page_templates();
// Sort based on key
ksort($templates);
foreach ( $templates as $template_name => $template_filename ) {
    echo "$template_name ($template_filename)<br />";
}
?>

Also see asort() if you need.

  • @ the_dramatist ... Thanks. I wasn't aware of ksort/asort/krsort. – glvr May 25 '17 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.