Hi! I am Geshan Manandhar, a Bachelor's of Information Management (BIM) graduate. Currently I am working as a developer (PHP) at Young Innvovations Pvt Limited.
Jun
27
Submitted by geshan on Fri, 06/27/2008 - 22:23
You might be wondering what is the use of this article / tutorial when Drupal system of handling title and teasers is so good and there are modules like node teaser to customize the teaser as per your need. But lets picture a scenario you are developing a website with a custom front page (required by majority of websites) so you have created your page-front.tpl.php and created a panel kicking out your left and right sidebar selection and example below.
Now the problem is you can tell the people who create nodes to put a title of just five words or less and a teaser of 300 characters or less so what to do? If they put a node title of more than say 25 letters your design will be hampered so the solution is tame your Drupal title how is what you get below:In your node.tpl.php (recommended node-content_type.tpl.php for better results)
The original code to render node title is something like below (I’m using Amor_Azul theme, just find the $title printed in your theme’s node.tpl.php):

Change it to or add:

Now the trick is if the page is Front page and the displayed content is teaser then I get the string length of the title of the node and if the string length is more than 25 characters I print … after it else I print the title. Easy it seems, I think the page==0 is for the full content display.
Ok time to tame the teaser for the front page, the original code for the $content is given below with the tweak:
Original code for content in node.tpl.php in your theme folder (I’m using Amor_Azul theme and recommend using node-content_type.tpl.php to try this)
Change it to:
So what did I do here?:-) Easy on it, in the content div I checked if teaser is being displayed in the front page if yes again the string length tweak as its content I used two hundred and ninety characters which suited my need. If teaser length was more than 290 I added … to the teaser else printed the teaser. In case it was not the front page and teaser the regular content was printed. Or the output for the above experiment that worked successfully is below:

Sorry for the code in pictures, download the code text Here.
Part 2 of this tutorial can be found here, part 2 is for custom node types with body and other fields.
Some add-ons you can try, a problem may be what if the 25th character of the title is a space then it’ll display “title …” looking awkward so it can be handled. Another possible extension is adding link to the full node in the “…” in the teaser’s … (continued pointer). Well try out for yourself, if you tweak and get something interesting don’t forget to comment here.
10 comments
Thanks for this Geshan,
My problem is though that every summarise (crop or trim) code I use in a node-teaser.tpl or 'if $teaser' ends up breaking tags (like italicize) applied to a bunch of text. I have users who love applying em to whole paragraphs!
Is there a way to prevent tags from being broken?
Or rather this module...
http://drupal.org/project/teaserbytype
Or just get the module....
http://drupal.org/project/nodetypeviews
Dear Geshan,.. thank,.. But I'm using Drupal 6 now,.. How to make it work on Drupal 6? Can we do it?
Dear Mariyana,
Thanks for those wonderful comments. Check out other Drupal related posts on my blog.
I guess that when pasting the code you might have accidentally typed FFDD or other div may have been typed. Check your content_type.tpl.php file at the place where you pasted the code. I guess that may be the reason.
Check it, that may solve your problem.
Geshan
Hi Geshan,
Thanks for the help you give to such newbies like me which want to do something more than the ordinary things Drupal offers.
Your code works fine but i get something like FFDD in a square after every teaser. What does cause this and how can I remove it?
Regards,
Mariyana
Here is a word counter solution to this, here you can count the no. of words to display than characters. Link to the code text file is here. The code is of the content div as above.
Geshan
I'll look into it if I get the solution I'll update the post, thanks for the suggestion.
Or how about instead of trimming by character size only, we first trim by character size, and detect the last space character position and then trim upto that place only,
the reason being, in the image you can see the last f... now this is not really nice,
Get Your dancing shoes ...
Very good help, the code in the txt file solved my problem. Thanks a lot.
Steve