Make Money on the Internet
Make Money on the Internet

How to: Fix Long Post Titles in the Sidebar

Written by Abdulrehman on April 3rd, 2009

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:

<?php echo $before_widget; ?>
<?php echo $before_title . $title . $after_title; ?>
<ul>
<?php  while ($r->have_posts()) : $r->the_post(); ?>
<li><a href=”<?php the_permalink() ?>”><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li>
<?php endwhile; ?>
</ul>
<?php echo $after_widget; ?>

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

<?php echo $before_widget; ?>
<?php echo $before_title . $title . $after_title; ?>
<ul>
<?php  while ($r->have_posts()) : $r->the_post(); ?>
<li><a title=”<?php $ba = the_title(”,”,false); echo $ba ?>” href=”<?php the_permalink() ?>”>
<?php
if((strlen($ba)) >=37)
echo substr($ba,0,33) . “…”;
else
the_title();
?>
</a></li>
<?php endwhile; ?>
</ul>
<?php echo $after_widget; ?>

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.

Related Articles:

Posted in Blogging |

8 People have expressed themselves on“How to: Fix Long Post Titles in the Sidebar”

  1. Thanks for this greatest tips. You help many bloggers to fix their long title on the sidebar.

    [Reply To This Comment]

  2. Ex Back says:

    Hey, nice tips. I’ll buy a glass of beer to that man from that chat who told me to visit your blog :)

    [Reply To This Comment]

    Abdulrehman Agha Reply:

    Are you really reading or just trying to get on the Top Commentators list?

    [Reply To This Comment]

  3. Wow, thanks, my blog looks like an absolute mess as some of my titles are like 60 characters and they just make the whole widget look messy, so this will certainly help me fix that up

    [Reply To This Comment]

  4. Post names should be short anyway so I guess instead of playing around with code bloggers should focus more on making titles more interesting and shorter. Remember: it’s the title that attracts your readers.

    [Reply To This Comment]

    Abdulrehman Agha Reply:

    The problem is not the shortness, it is the line breaking which is being prevented. Even an article with 6 words can break into the next line if the sidebar is small. Hope you get my point.

    [Reply To This Comment]

  5. Kikolani says:

    Thanks for the tip. I think bloggers go for longer titles sometimes because of the keywords in the URL. Those can come up pretty high in search results quickly.

    ~ Kristi

    [Reply To This Comment]

  6. Dear Abdul,
    Nice effort by you for online earning .Keep it up,
    Visit my site may be you find it useful Homebased Money

    [Reply To This Comment]

Leave a Reply

:mrgreen: :| :twisted: :arrow: 8O :) :? 8) :evil: :D :idea: :oops: :P :roll: ;) :cry: :o :lol: :x :( :!: :?:

Recent Comments


  • Abdulrehman Agha: Thank you very much. Delegate of malaysia. :)
  • Amr Leghari: Hehe, Happy Birthday; an extremely belated one too. Like your blog :-] Malaysia(from SMAMUN)
  • Joggler: Well, requested a 59$ payment on April 13th, still waiting. I’m not expecting to be paid since, luckily, I wasn’t stupid enough to...
  • Chamara: Hi My friend, wish u all the best, & good luck.
  • GEORGE: HELLO GUYS, THE BUX.TO IS THE BIGGEST SCAM IN THE INTERNET TODAY ! IF I CAN IT, I WILL SEND THESE PEOPLES IN THE PRISON !!

Recent Readers


About Abdulrehman Agha


Abdulrehman AghaI am Abdulrehman Agha, the guy behind 3arn.Net. I am 17 years old, and live in Rawalpindi, Pakistan. I'm in Grade 11 studying in Saint Mary's Academy. I started this blog to share my money making experiences with others through the internet.

I started with Paid to click websites till I made my very first blog which converted into this one soon enough. Through time I have reached different milestones with this blog and I have matured into a much different person learning much from the internet marketing field. Continue reading about me...