Friday, January 14, 2011

How to: Fix Long Post Titles in the Sidebar

Almost every blog that I visit uses the ‘Recent posts/Recent Articles’ widget which is a default widget in the wordpress files. When I was making this new theme for my blog, I wanted to automate it as much as possible. Did I ever tell you that I had to manually update the recent posts widget for my blog before I did this tweak? Well, you might think I’m crazy but the problem is that I’m a little too choosy while formatting and I don’t want even a single link in my sidebar to break into the next line, that is it should be coming in 1 line, whatsoever.
I always thought that it was a part of the theme that made that function and I wanted to cut it off at the character where it was going to break into the next line. So by the time I was done with my theme, I discovered that the function was not a part of the theme, in fact it was a part of the default wordpress ‘engine’ files.
So after searching each one of them I discovered that it was in the widgets.php file. If anyone of you wants to make the long post titles break down before they move to the next line, then be sure of what you’re doing as messing up with the wordpress files can mess up the functionality of you blog. I’m not trying to scare you, but I must warn you. Moreover, you must also know how to open and edit your blog’s wordpress files which are located at your host.
So once you’re at your host and are at the browser, open the folder where you installed your wordpress blog, then open wp-includes folder and then click on the file called widgets.php and edit it (whichever way your host supports). Once it is open look for this part:



    have_posts()) : $r->the_post(); ?>
  • ”>



Once you’ve found it, replace it with this:



Now, you need to check the things that are in bold, in my case I want the maximum size of the post title to be 37 because of the width of my sidebar. So if the post title is already less than 37, it will display the original post title else, it will take it’s first 33 characters and add ‘…’ at it’s end. If you change the first number in bold, just subtract 4 from it and put the resulting number in the second one. You can see this in action in this blog’s sidebar. Hope this helps the people who wanted to get rid of the word wrap ruining their design, and if you need any help with this just post a comment and I shall be glad to help.

1 comment: