Add "Read More" link after Drupal node teaser

Do you want to display a "Read More..." link inlined with the blog post's teaser?

I found the Drupal Read More Link module. I couldn't make this module work with content which is created using WYSIWYG editors. Actually, there is an issue related to this problem.

This is one solution.

  1. <?php
  2.  
  3. function _mytheme_read_more_link(&$html, $nid){
  4. $link = l(t('Read More >'), "node/$nid", array('attributes' => array('class' => 'read-more')));
  5. //check if the string ends with tag closing bracket
  6. if(substr($html, -1) === '>') {
  7. //get the position of the tag opening bracket for the last tag
  8. $pos = strrpos($html, '<', -1);
  9. //insert the text that position
  10. $html = substr_replace($html, $link, $pos, 0);
  11. }
  12. else {
  13. //no tag found - append the link HTML
  14. $html .= $link;
  15. }
  16. }
  17.  
  18. ?>

What this function does is to check whether there is a closing tag bracket in the end of the string. If such bracket was found, the "read more" link is inserted just before opening bracket of the last tag.

You could call this function from the template.php file as follows:

  1. <?php
  2. function mytheme_preprocess_node(&$vars) {
  3. if($vars['node']->type == 'blog'){
  4. if ($vars['teaser'] ){
  5. _mytheme_read_more_link($vars['content'], $vars['node']->nid);
  6. }
  7. }
  8. }
  9. ?>

It is possible that the WYSIWYG editor has inserted additional <br/> tags in the end of the teaser so you should temporally turn off formatting of the text to check the generated HTML code.

Hope this function helps you!