HowTo – WordPress The Excerpt Verses The Content

Filed under: Wordpress Tips

When you are developing or redesigning a wordpress theme there are times that you want to show different amounts of content to your visitor.

Every time that a webpage is generated from wordpress content you are making use of the Loop.  The Loop on your front or index.php page may show 10 full posts or it may show excerpts of the content to get the reader interested or it may show one full post, 4 excerpts and 5 permalink titles to more posts.

The way you design the output is up to you.

Areas that you may want to use excerpts could be your Home Page (home.php) a category page template 123-category.php, archive pages, search results or where ever you need to include an excerpt that you manually inserted into your post when it was published.

In general the_content will show all of the post and the_excerpt will show the first 55 words of the post.

In the_excerpt you won’t see images or special html formatting expect plain text

WordPress will generate an excerpt from your main the_content if a manual excerpt was not added to your post when you wrote it.

The <!–  more –> tag can be used to display less of the_content in your index archives category and other pages but it must be manually set in each post.

Lets Test How It Works

Go to your theme directory and open your index.php file which is your front page for your site.

Find the line within the loop that displays the_content and change that to the_excerpt. Now save the file and refresh your home page.

That was pretty easy wasn’t it.

How Can I Change the Length of the_excerpt

The default is 55 words but you can modify that length in your functions.php file.

If you do not have a functions.php file in your theme just make a text file and provide that name

Start the file with <?php and the last line must be ?> no white space lines after the ?> or you will get errors.

[php]
<?php
function new_excerpt_length($length) {
return 30;
}
add_filter(‘excerpt_length’, ‘new_excerpt_length’);
?>
[/php]

Add a link to the rest of the post after the_excerpt

To add a link to the rest of the post you can use the following function in your functions.php file

[php]
<?php
function new_excerpt_more($more) {
global $post;
return ‘<a href="’. get_permalink($post->ID) . ‘">’ . ‘Read the Rest…’ . ‘</a>’;
}
add_filter(‘excerpt_more’, ‘new_excerpt_more’);
?>
[/php]