Select Page
How to limit characters in widget titles

How to limit characters in widget titles

From some time i’ve been wondering how to make the wiget titles shorter.

The thing that bummed me out was that the widget “recent posts” was inside a core file from wordpress. The problem with this is the fact that if you change wordpress core files, at the next update, the file would get rewritten. And is a bad practice anyway… So after some long research on how to get this done, i’ve decided to take a break, as i couldn’d find a solution that woked. At a later time and with a fresh mind i took another aproach on this.

I tought that it must be some plugin that i can use in order to have a new widget with the same functions that the core file has, and to write that down. I found Custom Recent Post Widget can do this for me and i was so happy. Now all i had to do was to find the files that i needed to edit and to write some code over there… and that was it.

Here is how i did just that

1. Install and activate Custom Recent Post Widget
2. With your FTP client search inside the plugin folder search for includes > crpw-cat.php and open this file
3. Now you shoud find the class crpw-item, and inside that search for the “a hreh=” tag (about line 52) like below

1
<a  href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent link to <?php the_title_attribute(); ?>" class="crpw-title"><?php the_title(); ?>

4. And replace the code above with this one

1
2
3
4
5
<a  href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent link to <?php the_title_attribute(); ?>" class="crpw-title"><?php  if ( get_the_title() ) {
                    $t = get_the_title();
                }
        echo mb_strimwidth(get_the_title(), 0, 50, '...');
                ?></a>

What i’ve done was to replace the “the_title” with “get_the_title” and added the “echo” to it and stated that i want the title to be displayed with a maximum of 50 charaters. You can replace the value “50” whith whatever value you want.

If this worked for you leave a comment, share or save this article for a later use.