I read the thread here about this…
http://wordpress.org/support/topic/truncate-titles-in-recent-posts-widget?replies=8
but the suggested solution didn’t work, because I wasn’t sure what to do with the code.
Can anyone give me the full code using the code from the pastebin link, and here: http://wordpress.org/support/topic/post-title-char-count?replies=5
Shortening the titles to 25 characters please? I’m really not good at PHP.
It may be easier to make a plugin since the code is already there, so anyone can choose how many characters to limit the titles to?
Thanks so much!
I wasn’t sure if i would answer or simply vote to close this topic, i feel i sufficiently asnwered the original topic you’re referring to, i’d only be repeating myself here.
That said, here’s an upto date version you can use(only took me 2 mins), you only need implement your own code to truncate the titles in the widget, i’ve marked the area of code for you.
You’ll need to decide how you want to truncate the title, either by a set amount of words or as you said in your question, by number of characters, in any case each approach has some drawbacks, which is why i’m leaving you to decide which you want to implement and why..
The area you’ll need to update has a comment next to it that looks as follows..
Small note, just make sure to use
get_the_title()
when you need to manipulate the title, because that will give you a return value instead of a printed one(which you’ll get withthe_title()
).Here’s a link to a topic on StackOverflow with regard to truncating text, and also a google link with a mountain of links you can study to get ideas about which approach to take with truncating.
https://stackoverflow.com/questions/965235/how-can-i-truncate-a-string-in-php
http://www.google.comk/search?q=php+truncate+string+to+length
NOTE: This is a replacement for the existing recent posts widget, you’ll not lose any you’ve setup already, it will inherit and take over the settings from the default recent posts widget.