Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress - the_title() limit character in standard widget

Tags:

wordpress

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?

like image 892
Wordica Avatar asked Aug 31 '25 06:08

Wordica


2 Answers

this one should work

echo mb_strimwidth(get_the_title(), 0, 40, '...');
like image 76
dewaz Avatar answered Sep 02 '25 21:09

dewaz


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.

like image 39
TheBosti Avatar answered Sep 02 '25 23:09

TheBosti