Display First Full Entry And Then Following Entries As Excerpts

Posted by Nile | Posted in Tutorials | Posted on 11-20-2009 | 7 Comments

Tags:

I like to use this code to display the first full entry and then the rest of my entries as excerpts from my main page. I have used this many times, even on my other site Blondish.net. This is not hard to do and only requires that you replace a few code snippets. You can apply this to your main index template.

Look for the following code in the loop in your:


<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

Replace with the following.

<?php if (have_posts()) : ?>
<?php $postcount = 0; // Initialize the post counter ?>
<?php while (have_posts()) : the_post(); //start the loop ?>
<?php $postcount++; //add 1 to the post counter ?>

This code allows your blog to see how many posts you have.

2. Look for the following code. You are going to be replacing the_content


<?php the_content('Read the rest of this entry &raquo;'); ?>

with:


<?php if ($postcount == 1) : // if this is the first post ?>
<?php the_content('Read the rest of this article ->'); //Show the full post ?>
<?php else : //if this is NOT the first post ?>
<?php the_excerpt(); ?>
<?php endif; //end of the check for first post - other posts?>

The code basically tells your blog to show the first as the full entry and the following posts as excerpts.

This snippet is seen in action here at WP Addict.

Editing The Number Of Words For The Excerpt

Posted by Nile | Posted in Tutorials | Posted on 11-06-2009 | Comments Off

Tags:

Some people might think it is the number of lines you have to define to edit how long or short your excerpt in your WordPress article. It is not. It is defined by how many words. WordPress by default picks the first 55 words as your excerpt. However, if you wish to edit to make it shorter or longer:

EDIT: Please note, this way changes the core files and upgrades will only change it back. (Thanks Mike Little!)

1. Go to your wp-includes folder and find the formatting.php file

2. Look for the group of code like

/**
* Generates an excerpt from the content, if needed.
*
* The excerpt word amount will be 55 words and if the amount is greater than
* that, then the string '[...]' will be appended to the excerpt. If the string
* is less than 55 words, then the content will be returned as is.
*
* @since 1.5.0
*
* @param string $text The exerpt. If set to empty an excerpt is generated.
* @return string The excerpt.
*/
function wp_trim_excerpt($text) {
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text);
$excerpt_length = apply_filters('excerpt_length', 55);
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, '[...]');
$text = implode(' ', $words);
}
}
return $text;
}

3. If you see $excerpt_length = apply_filters(‘excerpt_length’, 55); change the text length which by default is 55 to the number of words you prefer.

—–

Mike Little suggests a better way with a lot less code and hassle involved! :)

Apply a filter! Alright the above code hints to you that you are applying a filter, so instead, you can leave the core files and formatting.php file alone.

Instead:

1. Go to your functions.php of your theme template and add the following


add_filter('excerpt_length', 'my_excerpt_length');
function my_excerpt_length($len) { return 75; }

Now your excerpts will be the length you want!