I am having an issue with a WP_query loop (full code below).

Whenever I just run echo $post->post_title it prints out the title just nicely.

But if I try to do something like the following: echo substr($post->post_title,0,1) it can't display special characters such as ø æ å. It is as if it splits the special character into two - what the?

I am saying it splits it because, if I try to run echo substr($post->post_title,0,2) (printing out 2 characters), it prints the character correctly, but only prints one.

Here's my full code for the loop:

$args = array( 
  'orderby'   => 'post_title',
  'order'     => 'ASC',
  'post_type' => 'ord',
  'posts_per_page' => -1
$loop = new WP_query($args);

$mainArray = array_chunk($loop->posts, ceil(count($loop->posts) / 4)); // Array af arrays

foreach ($mainArray as $array) {
$first_letter = '';

  echo "<div class='col ordbog-column'>";
  foreach($array as $post) {
    $current_letter = strtoupper(substr($post->post_title,0,1));

    if($current_letter != $first_letter) {
      echo "<h3 class='letter' id='letter_$current_letter'>$current_letter</h3>";
      $first_letter = $current_letter;

    $html = '<a href="'.get_permalink().'" class="ord">'.get_the_title().'</a><br/>';
    echo $html;
  echo "</div>";

How do I fix this loop so I can correctly display just one letter (regardless of which) as a headline over the content being listed?

2 Answers 2


Have you tried mb_substr() replace substr function with mb_substr

$current_letter = strtoupper(mb_substr($post->post_title,0,1));

This is a general PHP problem rather than a WP problem.

Specifically, those characters are multibyte characters, and grabbing the first character via substr slices it in half

Instead, try using mb_substr

  • I thought as much, but wasn't aware that mb_substr existed (although I tried plenty mb_ functions). Thank you!
    – Martin
    May 9, 2018 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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