Custom teaser length by View using node.tpl.php

Last modified: September 6, 2008 - 08:17

One way to vary teaser lengths is to check the current View with a modified node.tpl.php modify the output based on this

In this example a teaser of length 75 or 150 will be shown for the Views "someviewname" and "anotherviewname" respectively

It is from D6 Garland

**This code has not been tested**

<?php
// $Id: node.tpl.php,v 1.5 2007/10/11 09:51:29 goba Exp $
?>


<?php global $current_view; ?>


<?php
$myTeaser1
= strip_tags($node->content['body']['#value']);

if (
strlen($myTeaser1) > 75) {
$myTeaser1 = drupal_substr($myTeaser, 0, 75) . '...';
}


$myTeaser2 = strip_tags($node->content['body']['#value']);

if (
strlen($myTeaser2) > 150) {
$myTeaser2 = drupal_substr($myTeaser, 0, 150) . '...';
}

?>



<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?>">

<?php print $picture ?>

<?php if ($page == 0): ?>
  <h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2>
<?php endif; ?>

  <?php if ($submitted): ?>
    <span class="submitted"><?php print $submitted; ?></span>
  <?php endif; ?>

  <div class="content clear-block">

<?php
if ($current_view->name == 'someviewname') {
print
$myTeaser1;
}
elseif (
$current_view->name == 'anotherviewname') {
print
$myTeaser2;
}
else {
print
$content
}
?>


  </div>

  <div class="clear-block">
    <div class="meta">
    <?php if ($taxonomy): ?>
      <div class="terms"><?php print $terms ?></div>
    <?php endif;?>
    </div>

    <?php if ($links): ?>
      <div class="links"><?php print $links; ?></div>
    <?php endif; ?>
  </div>

</div>

please feel free to fix or improve

many thanks to betarobot, nearlythere, and everyone else in the irc rooms

Slight Changes...

avr - October 12, 2008 - 02:16

I got this working on a local install, but it took a few changes.

First, the variable names changed initially.

$myTeaser1 became $myTeaser on this line:

$myTeaser1 = drupal_substr($myTeaser, 0, 75) . '...';

It should be $myTeaser1 and $myTeaser2 throughout. Like this:

<?php
$myTeaser1
= strip_tags($node->content['body']['#value']);

if (
strlen($myTeaser1) > 75) {
$myTeaser1 = drupal_substr($myTeaser1, 0, 75) . '...';
}


$myTeaser2 = strip_tags($node->content['body']['#value']);

if (
strlen($myTeaser2) > 150) {
$myTeaser2 = drupal_substr($myTeaser2, 0, 150) . '...';
}

?>

Also, the View name is incorrectly called by:

if ($current_view->name == 'someviewname')

The correct call of the 'view' variable is:

if ($view->name == 'someviewname') (notice the drop of 'current')

Like I said, this is only on a local/testing install. I'm currently running Drupal 6.4 with Views 6.x-2.0-rc1.

I'm using this to change the view of teaser lengths on 2 views on a frontpage. In the views setting, I've set each view to display 'Row style: Node' as opposed to "Fields."

The result on the frontpage is 1 "Feature" and 3 "Headlines" that follow. Now, each displays a different teaser length and works like a charm (so far). Anybody else have thoughts?

I need it soo bad, but it's

panji - November 9, 2008 - 02:25

I need it soo bad, but it's not working for me,.. :( So, what is wrong with me? is it about the views parameters? I'm using Views 6.x-2.1

If i by pass the code, only replace the print $content with $myTeaser1 , it's work but the imagefield won't show up...

Thanks

 
 

Drupal is a registered trademark of Dries Buytaert.