0

i'm sorting posts alphabetically with first letter above posts and works fine, but i need to display some slavic characters like ć,č,ž,đ...(UTF-8).

This is the code:

<?php
                $args = array(
                  'orderby' => 'title',
                  'order' => 'ASC',
                  'caller_get_posts' => 1,
                  'posts_per_page' => -1,
                 );
                query_posts($args);
                if (have_posts()) {
                   $curr_letter = '';
                   while (have_posts()) {
                      the_post();
                      $this_letter = strtoupper(substr($post->post_title,0,1));

                      if ($this_letter != $curr_letter) {
                         echo "<span>$this_letter</span>";
                         $curr_letter = $this_letter;
                      }
                      ?>

                      <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>

                 <?php }
                }
                ?>

I tried with this:

$this_letter = mb_strtoupper(mb_substr(apply_filters('the_title',$post->post_title),0,1));
$this_letter = mb_convert_encoding($letter,'UTF-8', mb_detect_encoding($this_letter) );

instead of this:

$this_letter = strtoupper(substr($post->post_title,0,1));

but doesn't work.

I appreciate your help.

  • Do not use any convert routines. What output do you get? See http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored for discussion of common problems. – Rick James Oct 16 '16 at 04:57

1 Answers1

0

This should work:

<?php
            $args = array(
              'orderby' => 'title',
              'order' => 'ASC',
              'caller_get_posts' => 1,
              'posts_per_page' => -1,
             );
            query_posts($args);
            ?>
            <?php    
            if (have_posts()) {
               $curr_letter = '';
               while (have_posts()) {
                  the_post();
                   $this_letter = strtoupper(mb_substr(mb_convert_encoding($post->post_title,'UTF-8'),0,1));

                  if ($this_letter != $curr_letter) {
                     echo "<span>$this_letter</span>";
                     $curr_letter = $this_letter;
                  }
                  ?>

                  <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>

             <?php }
            }                
            ?>