• strict warning: Non-static method view::load() should not be called statically in /home/mosettis/public_html/sites/all/modules/views/views.module on line 1113.
  • strict warning: Declaration of views_handler_field::query() should be compatible with views_handler::query($group_by = false) in /home/mosettis/public_html/sites/all/modules/views/handlers/views_handler_field.inc on line 1147.
  • strict warning: Declaration of content_handler_field::element_type() should be compatible with views_handler_field::element_type($none_supported = false, $default_empty = false, $inline = false) in /home/mosettis/public_html/sites/all/modules/cck/includes/views/handlers/content_handler_field.inc on line 229.
  • strict warning: Declaration of views_handler_argument::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/mosettis/public_html/sites/all/modules/views/handlers/views_handler_argument.inc on line 917.
  • strict warning: Declaration of views_handler_argument::query() should be compatible with views_handler::query($group_by = false) in /home/mosettis/public_html/sites/all/modules/views/handlers/views_handler_argument.inc on line 917.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/mosettis/public_html/sites/all/modules/views/handlers/views_handler_filter.inc on line 587.
  • strict warning: Declaration of views_handler_filter::query() should be compatible with views_handler::query($group_by = false) in /home/mosettis/public_html/sites/all/modules/views/handlers/views_handler_filter.inc on line 587.
  • strict warning: Declaration of views_plugin_query::options_submit() should be compatible with views_plugin::options_submit($form, &$form_state) in /home/mosettis/public_html/sites/all/modules/views/plugins/views_plugin_query.inc on line 169.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/mosettis/public_html/sites/all/modules/views/plugins/views_plugin_row.inc on line 136.
  • strict warning: Declaration of views_plugin_pager_none::post_execute() should be compatible with views_plugin_pager::post_execute(&$result) in /home/mosettis/public_html/sites/all/modules/views/plugins/views_plugin_pager_none.inc on line 69.
  • strict warning: Non-static method view::load() should not be called statically in /home/mosettis/public_html/sites/all/modules/views/views.module on line 1113.
  • strict warning: Declaration of views_handler_sort::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/mosettis/public_html/sites/all/modules/views/handlers/views_handler_sort.inc on line 165.
  • strict warning: Declaration of views_handler_sort::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/mosettis/public_html/sites/all/modules/views/handlers/views_handler_sort.inc on line 165.
  • strict warning: Declaration of views_handler_sort::query() should be compatible with views_handler::query($group_by = false) in /home/mosettis/public_html/sites/all/modules/views/handlers/views_handler_sort.inc on line 165.
  • strict warning: Declaration of semanticviews_plugin_style_default::options() should be compatible with views_object::options() in /home/mosettis/public_html/sites/all/modules/semanticviews/semanticviews_plugin_style_default.inc on line 232.
  • strict warning: Declaration of semanticviews_plugin_row_fields::options_validate() should be compatible with views_plugin_row::options_validate($form, &$form_state) in /home/mosettis/public_html/sites/all/modules/semanticviews/semanticviews_plugin_row_fields.inc on line 117.
  • strict warning: Non-static method view::load() should not be called statically in /home/mosettis/public_html/sites/all/modules/views/views.module on line 1113.
  • strict warning: Non-static method view::load() should not be called statically in /home/mosettis/public_html/sites/all/modules/views/views.module on line 1113.
  • strict warning: Non-static method view::load() should not be called statically in /home/mosettis/public_html/sites/all/modules/views/views.module on line 1113.

Custom teasers in Drupal 6 with views 2

Refine the text lenght of your views teaser texts

Update: With the new releases of Views it is possible to do all the trimming and html stripping directly from the views UI.

While I was developing this website I run into the need of having total control over the teasers of my contents especially while using views to extract them.  Probably if you have just one content type you can manage it with the default option in drupal administration.

The simplest solution I have found is to use the neat_trim function. So what you have to do is just copy and paste the following function into your theme template.php file (just remember not to copy the "?>" php closure):

 

<?php

function neat_trim($str, $n, $delim='&hellip;') {

$len = strlen($str);

if ($len > $n) {

preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches);

return rtrim($matches[1]) . $delim;

}

else {

return $str;

}

}

?>

 

Now you just need to go on each template file where you want this function (you can easily find and create them inspecting your views from the views admin panel) and put the following code:

 

<?php

$string = neat_trim($output, 100);

print $string;

?>

 

This will print out the content limited to 100 characters or the full content in case it has less then 100 characters. A small extra tip if you are using a WYIWYG editor or if you have some HTML code in your page when you create your content is to add a strip_tags so as to remove any html tag in you text to avoid unwanted results on your page. So the code will look like that:

 

<?php

$output = strip_tags($output);

$string = neat_trim($output,100);

print $string;

?>

 

This is how the news on the home page and the blog block teaser were created in this website.

19 Jan 2009