use as follows (for demonstration only) :
function getInitials($name){
//split name using spaces
$words=explode(" ",$name);
$inits='';
//loop through array extracting initial letters
foreach($words as $word){
$inits.=strtoupper(substr($word,0,1));
}
return $inits;
}
$currval = "";
foreach($children as $child){
//print_r($child);
$permalink = get_permalink($child->ID);
$initial = getInitials($child->post_title);
if($initial!='' && $currval != $initial) {
$currval = $initial;
echo '<optgroup>'.$initial.'</option>';
}
echo '<option value="'.$permalink.'">'.$child->post_title.'</option>';
}
Not checked though.. please check and return if you face any prob
In the case you need to show all alphabets, i can give you an idea to do so
function getInitials($name){
//split name using spaces
$words=explode(" ",$name);
$inits='';
//loop through array extracting initial letters
foreach($words as $word){
$inits.=strtoupper(substr($word,0,1));
}
return $inits;
}
$currval = "";
$alp = array('A'...'Z')
foreach($alp as $abc) {
foreach($children as $child){
//print_r($child);
$permalink = get_permalink($child->ID);
$initial = getInitials($child->post_title);
if($initial!='' && $currval != $initial && $initial == $abc) {
$currval = $initial;
echo '<optgroup>'.$initial.'</option>';
}
echo '<option value="'.$permalink.'">'.$child->post_title.'</option>';
}
}