0

I have a alphabetic list of custom taxonomies (Topics), all taxonomies are grouped by a index letter - A, B, C etc. The list uses the first letter of "Topic", to show as a index letter for each alphabetic group.

The alphabetic groups works perfect, but when it shall show the Danish letters Æ, Ø and Å, I get faulty letters like � instead of Å. I the database is set to utf8_danish_ci and so is wp-config.php

I really cannot figure out how to control $letter to show Æ Ø Å. Any help is much apreciated :)

enter link description here

Code used:

<?php $list = ''; 
$tags = get_terms( 'topic' ); 
$groups = array();
if( $tags && is_array( $tags ) ) {
foreach( $tags as $tag ) {
$first_letter = strtoupper( $tag->name[0] );
$groups[ $first_letter ][] = $tag;}
if( !empty( $groups ) ) {
foreach( $groups as $letter => $tags ) {
$list .= "\n\t" . '<div id="index-letter"><h2>' . apply_filters( 'the_title', $letter ) .'</h2></div>';
$list .= "\n\t" . '<ul>';
foreach( $tags as $tag ) {
$lower = strtolower($tag->name);
$name = str_replace(' ', ' ', $tag->name);
$naam = str_replace(' ', '-', $lower);
$list .= "\n\t\t" . '<li><a href="/topic/'.$naam.'">'.$name.'</a></li>';
}}}}else $list .= "\n\t" . '<p>Beklager, men ingen emner blev fundet</p>';print $list;
echo "</ul>";
?>
  • Check your blog charset in your admin settings and your theme or plugin files. It's probably some latin-0-9/ISO-* subset instead of UTF-8. – kaiser Mar 13 '14 at 14:11
  • I did that in the first place, and the solved alphabetic order, but not issue about special characters. Thanks though. – Athen Mar 14 '14 at 11:07
1

Use the Multibyte String functions, in this case mb_strtoupper().

This is a general rule: in WordPress, output is usually encoded in UTF-8. Avoid single-byte functions like strlen(), substr() and others when there is a better mb_* function.

  • Thank you very much, that looks correct. I must admit that I have pasted the code of some other post at Stackexchange, so I have not build it myself. When I replace: $first_letter = strtoupper( $tag->name[0] ); with: $first_letter = mb_strtoupper( $tag->name[0] ); the list works, but it does not output any of the special letters. All "normal" letters is listed ok. – Athen Mar 14 '14 at 9:41
  • Solution: $first_letter = mb_strtoupper( mb_substr($tag->name,0,1) ); – Athen Mar 14 '14 at 11:24

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.