Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top