I may be shortsighted to suggest this, but why can we just take the string content of a textfield we create as the body content and then use basic PHP string manipulation within contemplate to save the first 50.. 100 or 121 characters to a variable and then output that in the contemplate of our teaser? That way we can control the characters that are spit out per content type by using the same code in other contemplates.

Would this work?

Comments

roleychiu’s picture

well my hunch was right. Here's the code that you can put into your contemplate to customize any length of teaser you wish.

Hope this helps some people out

<?php 
$text=$node->content['body']['#value'];

function ShortenText($text) {
        // Change to the number of characters you want to display

        $chars = 60;
        $text = $text." ";
        $text = substr($text,0,$chars);
        $text = substr($text,0,strrpos($text,' '));
        $text = $text."...";
        return $text;
 }

print ShortenText($text);

 ?>

Source: http://www.totallyphp.co.uk/code/shorten_a_text_string.htm

roleychiu’s picture

edit: the above code works if there's only 1 node on the page. For multiple nodes, you need to put the function declaration in your template.php file so the function is globally accessible and not declared multiple times in the same page.

subir_ghosh’s picture

(for multiple nodes)

I have put this in my template.php

function ShortenText($text) {
        // Change to the number of characters you want to display

        $chars = 60;
        $text = $text." ";
        $text = substr($text,0,$chars);
        $text = substr($text,0,strrpos($text,' '));
        $text = $text."...";
        return $text;
}

And in the node-nodetype.php I am calling this:
<?php print ShortenText($text); ?>

Nothing appears except the "...".

-----------------------------
Subir Ghosh
www.subirghosh.in

subir_ghosh’s picture

Template.php code remains the same.

I am using this in the node.tpl
<?php $text=$node->content['body']['#value']; print ShortenText($text); ?>

-----------------------------
Subir Ghosh
www.subirghosh.in

subir_ghosh’s picture

What should one do if one wants to have two types of such teasers?

Once the character limit is set in the template.php file, it becomes applicable throughout the site.

-----------------------------
Subir Ghosh
www.subirghosh.in

joachim’s picture

Do you want different length teasers for different content types?
Try http://drupal.org/project/teaserbytype

(blowing own trumpet... ;)

roleychiu’s picture

lol why didn't I find your module sooner? So your module allows us to set any teaser length? even < 200? Does it truncate by char or does it retain the full word like in the code I posted?

joachim’s picture

It just gives the same possible lengths as core does. And it truncates in the same way as core -- uses the same function.

doublejosh’s picture

I really don't want to hack away.

joachim’s picture

Hmmm the number of people who want this is growing -- http://drupal.org/node/306141

I might have to bow to demands and implement something. Will ponder.