Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How to display excerpts in the homepage instead of full post automatically, without the need to insert <!--more--> in every post ?

I tried this plugin http://wordpress.org/extend/plugins/advanced-excerpt/ which or doesn't work or I can't use it.

Here's how I configure it http://wordpress.org/support/topic/plugin-advanced-excerpt-doesnt-work-installed-by-instructions-url-included?replies=1#post-2810718

This is my website.

I solved the problem using Michael's answer, which made my plugin work. http://wordpress.org/extend/plugins/advanced-excerpt/

share|improve this question
    
To Michael: This worked great, but can you tell me where to go and do if I also want to have the categories with excerpts ? Could it be by adding " and category " to the script <?php if (is_search() || is_home() ) : // Only display Excerpts for Search and posts page?> " Thank you –  user22718 Oct 20 '12 at 19:07
add comment

2 Answers

up vote 4 down vote accepted

general:

locate the template for the home page - often index.php - if not see http://codex.wordpress.org/Template_Hierarchy

find the_content() (possibly with some parameters in the brackets) and change that to the_excerpt() - http://codex.wordpress.org/Function_Reference/the_excerpt

specific for Twenty Eleven:

index.php points to the template part content.php; in content.php edit this line:

<?php if ( is_search() ) : // Only display Excerpts for Search ?>

change it to:

<?php if ( is_search() || is_home() ) : // Only display Excerpts for Search and posts page ?>

http://codex.wordpress.org/Conditional_Tags

share|improve this answer
    
Thanks, it worked.Any ideas how to make an excerpt longer and exclude images ? :) –  Tomas May 11 '12 at 10:13
    
@Tomas excerpts don't contain images by default. What you want sounds more like a limited content - you could try something like this: bavotasan.com/2009/… –  jammypeach May 11 '12 at 10:59
    
instructions how to change the excerpt length are included in codex.wordpress.org/Function_Reference/the_excerpt –  Michael May 11 '12 at 13:18
add comment

Insert this into your functions.php file to control the length of the excerpt by characters NOT words

function the_content_limit($max_char, $more_link_text = 'Read more &raquo;', $stripteaser = 0, $more_file = '') {
    $content = get_the_content($more_link_text, $stripteaser, $more_file);
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    $content = strip_tags($content);

   if (strlen($_GET['p']) > 0) {
      echo "<p>";
      echo $content;
      echo "&nbsp;<a href='";
      the_permalink();
      echo "'>".__("Read more")." &raquo;</a>";
      echo "</p>";
   }
   else if ((strlen($content)>$max_char) && ($espacio = strpos($content, " ", $max_char ))) {
        $content = substr($content, 0, $espacio);
        $content = $content;
        echo "<p>";
        echo $content;
        echo "...";
        echo "&nbsp;<a href='";
        the_permalink();
        echo "'>".$more_link_text."</a>";
        echo "</p>";
   }
   else {
      echo "<p>";
      echo $content;
      echo "&nbsp;<a href='";
      the_permalink();
      echo "'>".__("Read more")." &raquo;</a>";
      echo "</p>";
   }
}

Use it in your theme like this

<?php the_content_limit(245); ?>
share|improve this answer
2  
Do not use substr(), use mb_substr() instead. –  toscho Aug 6 '12 at 10:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.