WordPress – the_title() limit character in standard widget

I copy standard Recent Post widget to function.php, I unregister it and register my new class. In widget I see this function that is responsible for showing title of recent posts in A tag:

<?php while ( $r->have_posts() ) : $r->the_post(); ?>

        <li>
            <a href="<?php the_permalink(); ?>">
                <?php  if ( get_the_title() ) {
                    $t = the_title();
                    $t = substr($t, 0, 40); /// this doesn't work

                }else{
                    the_ID();
                }
                ?>
            </a>
...
...

But this substr doesn’t work – title is always display all. What I do wrong?

Related posts

Leave a Reply

2 comments

  1. You can alsp use mb_substr(), It works almost the same way as substr but the difference is that you can add a new parameter to specify the encoding type, whether is UTF-8 or a different encoding.

    Try this:

    $t =  mb_substr($t, 0, 40, 'UTF-8');
    

    LATER EDIT: change

     $t = the_title();
    

    with

    $t = get_the_title();
    

    You are using the_title instead of get_the_title to give it to an specific variable. And be sure to echo $t after all of this.