How To Modify “the_excerpt” in WordPress

These are a few pretty simple tricks that can actually be found if you go to WordPress and look in the reference for the_excerpt() function but I figured I would show you how I have customized it for my own installation.

The first two, in my opinion, work hand in hand because they actually modify the length and the “Read More” options of the_excerpt. These are typically the more two requested features that people like to change. For these just take the code below and drop it in your “functions.php” file located in your wp-content/themes/your-theme/ folder where “your-theme” is whatever theme you would like to have this work with.

Heres the code:

//Modify the length of the_excerpt
function new_excerpt_length($length) {
	return 150;
}
add_filter('excerpt_length', 'new_excerpt_length');
 
//Modify the "Read More" link of the_excerpt
function new_excerpt_more($more) {
	$new = str_replace("Continue reading ", "Read More", $more);
	$new = str_replace('<span class="meta-nav">→</span>', "", $new);
	return $new;
}
add_filter('excerpt_more', 'new_excerpt_more');

What this code is doing is created two new functions that will return a value to the_excerpt function when the filters are applied.

The first function if you haven’t guessed changes the length of the_excerpt so that it can show more than its default of 55 words.

The second is actually setup to take the default link that will send the user to the page with a “Read More” style link and allow the developer to change it. With the case of WordPress 3 they changed the end of the excerpt to say “Continue reading ” which we want to modify. As this changes you will just have to inspect the html of the page to find the current use and change the function replacing variables.

The other option that is available to change the_excerpt in a bigger way is to just create your own the_excerpt type of function within your “functions.php” folder. With the abilities of filters and other things its not typically as necessary.

WordPress Custom Comment Display

WordPress and I have become very good friends lately being that it is very versatile and a great open source blogging tool. One of the things that I find needs to be changed quite a bit for clients is how the comments are displayed once the new blog/news/information section of their website has been set up. The basics of it can be found here on the WordPress website and I am really just repeating what they already have but its something that I use enough I want to help others find it easier.

The first step is to go into the theme folder for whatever theme it is that you would like to customize. This is usually located in ~/wp-content/themes/theme-to-edit/ . Once you have found this folder you will want to open up the comments.php file and find the section that uses the wp_list_comments() function. Once you have found this you may see that it is encapsulated by <ol> or maybe <ul> tags. If the new way you would like to display the comments doesnt use these, get rid of them. Then you want to add ‘type=comment&callback=mytheme_comment’ into the function call so it should look like:

Just change “mytheme_comment” to whatever you want to call your new function. Now comes the fun part, open the functions.php file that is located in the same theme folder and open it. Once it is open you just add a new function to the list, its probably best to put it after the opening “if” statement in the file. The basic function that WordPress provides is this:

function mytheme_comment($comment, $args, $depth) {
 
$GLOBALS['comment'] = $comment; ?&gt;
&lt;li  id="li-comment-"&gt;
&lt;div id="comment-"&gt;
<div class="comment-author vcard">
&lt;?php echo get_avatar($comment,$size='48',$default='' ); ?&gt;
 
&lt;?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?&gt;</div>
comment_approved == '0') : ?&gt;
<em> </em>
<div class="comment-meta commentmetadata">
&lt;a href="comment_ID ) ) ?&gt;"&gt;</div>
<div class="reply">
 $depth, 'max_depth' =&gt; $args['max_depth']))) ?&gt;</div>
&lt;?php
}

Now personally, I think this looks like garbage so I usually change it to use some


or some background colors that alternate but you can do almost anything with it. As you will probably guess this function runs in a loop displaying the information it finds to be true in the statement. If you want you can use a counter to find even or odd comments and change the display or anything. Luckily when using this wordpress function you have a lot of freedom and can change anything you want, unlike some of the others. Here is a quick example of a function that I made in about 5 minutes to give you an example of things to eliminate and condense.

 

function mytheme_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?&gt;
&lt;li &gt;
<div class="comment-author"></div>
comment_approved == '0') : ?&gt;
<em> </em>
<div class="reply">
 $depth, 'max_depth' =&gt; $args['max_depth']))) ?&gt;</div>
&lt;?php
}

When I have some time I will put in an example of something with a counter for even and odd comments but otherwise go ahead and mess around, and good luck.

UPDATE!

Here is some basic code I just made up in about 5 minutes to show an example of some interval comments that could be used for some different backgrounds and such.

<code>
<ol class="commentlist">
 
&lt;dt class="commentid="comment-"&gt;</ol>
comment_status) : ?&gt;
<!-- If comments are open, but there are no comments. -->
 
<!-- If comments are closed. -->
</code>
<code>Comments are closed.
 
</code>