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.

How to Customize WordPress Page List Output

UPDATE: I uploaded a newer version of the function after a few tweaks that I made recently. Same functionality, just fixing issues.

When you have to customize a WordPress theme typically as a developer you are left with the options that the default WordPress functions give you. As I have found after customizing multiple themes to fit a users website design being able to create some different output can help significantly. Now this can take multiple functions but if you start to save them they can come it handy.

This is meant to be a replacement for the wp_list_pages() function that is used in WordPress currently. The documentation for this can be found at: http://codex.wordpress.org/Template_Tags/wp_list_pages

The function that I have just developed should help to customize page output of the navigation for any theme. I personally have used this to create custom output for the top level navigation and bottom level navigation. I have included the documentation in the function declaration. Please feel free to copy and use this as necessary. I would greatly appreciate it if you could leave my information in the comments.

 
&lt;?php
function wp_list_pages_custom($args, $custom_args, $debug = false) {
 
	//Usage
	/*
 
	This is meant to be a replacement function for wp_list_pages which can be found here:
 
	http://codex.wordpress.org/Template_Tags/wp_list_pages
 
	It takes in the same arguments as that function plus some other custom arguments to help
	change the output.
 
	$args
	--------------------------
	This is the same as the wp_list_pages function, a link can be found above
 
	$custom_args
	--------------------------
	This is where the user can customize the way the output is displayed, options can be found
	below.
 
	$debug
	--------------------------
	This is set to false by default, if you would like to change it to true you will see output
	at a few places. This is mainly for testing purposes if you need to change something.
 
 
	The custom arguments have the following default options:
 
	 "",
	'tag_close'		=&gt; "",
	'use_link'		=&gt; 0,
	'use_class'		=&gt; 0  ); ?&gt;
 
        Options:
 
	tag_open
	---------------------------
	This can take any opening string you wish to use. This could be something like "<p>" or "<div class='page_item'>".
 
	tag_close
	---------------------------
	This can take any opening string you wish to use. This could be something like "</p>" or "</div>".
 
	use_link
	---------------------------
	This takes a boolean value in numeric format. 0 = false, 1 = true.
 
	use_class
	---------------------------
	This takes a boolean value in numeric format. 0 = false, 1 = true.
 
 
	Example usage:
 
	&lt;?php
	echo wp_list_pages_custom(&quot;sort_column=menu_order&amp;echo=0&amp;title_li=&quot;, &quot;tag_open=<p>&amp;tag_close=</p>&amp;use_link=1&amp;use_class=1");
	?&gt;
 
	*/
 
	//Default variables
	$tag_open = "";
	$tag_close = "";
	$use_link = 0;
	$use_class = 0;
	$custom_pages = "";
	$default_echo = 0;
	$default_pages = "";
	$get_args = array();
 
	//Get custom arguments
	parse_str($custom_args, $get_args);
	$tag_open = (array_key_exists('tag_open',$get_args)?$get_args['tag_open']:"");
	$tag_close = (array_key_exists('tag_close',$get_args)?$get_args['tag_close']:"");
	$use_link = (array_key_exists('use_link',$get_args)?$get_args['use_link']:"");
	$use_class = (array_key_exists('use_class',$get_args)?$get_args['use_class']:"");
 
	//By default dont echo, just return value
	$default_echo = 0;
	if(preg_match("/^(.*?)(echo=([0-9]))(.*?)$/msi", $args, $match)) {
		$default_echo = $match[3];
	}else{
		$default_echo = "1";
	}
	if($debug == true){
		echo "Default Echo Argument: $default_echo<br />";
	}
 
	//Replace the echo so we can alter it
	if(preg_match("/^(.*?)(echo=[0-9])(.*?)$/msi", $args)) {
		$new_args = preg_replace("/^(.*?)(echo=[0-9])(.*?)$/msi", "$1"."echo=0"."$3", $args);
	}else{
		$new_args = $args."&amp;echo=0";
	}
	if($debug == true){
		echo "Arguments Passed: $new_args<br />";
	}
	$default_pages = wp_list_pages($new_args);
	if($debug == true){
		echo "Before Alteration Output: $default_pages<br />";
	}
 
	//Change the output to use specified tags
	if(preg_match_all("/^(<li>)<a>(.*?)</a>(</li>)$/msi", $default_pages, $matches, PREG_SET_ORDER)) {
		foreach($matches as $match) {
 
			//Choose whether to use the default classes, it will insert into the tag
			if($use_class == 1) {
				$temp_tag = preg_replace("/&gt;/msi"," class="".$match[2].""&gt;", $tag_open);
				$custom_pages .= $temp_tag;
			}else{
				$custom_pages .= $tag_open;
			}
 
			//Check how to display the text (with or without link)
			if($use_link == 1){
				$custom_pages .= "<a>".$match[4]."</a>";
			}else{
				$custom_pages .= $match[4];
			}
 
			$custom_pages .= $tag_close;
		}
	}
	if($debug == true){
		echo "After Alteration Output: $custom_pages<br />";
	}
 
	//Output
	if($default_echo == 1)
		echo $custom_pages;
	else
		return $custom_pages;
 
}
?&gt;

To implement this function all that a user must do is copy the function below and paste it into the bottom of the functions.php file found in the theme directory that the current WordPress installation is using. This is a generalized installation method so be careful to backup that file before changing it. If it is not an extremely customized theme the function should be able to be pasted at the bottom of the functions.php page and used thereafter.

As a side note, this has not been developed to deal with the “depth” option for the wp_list_pages() function. As it is it will most likely replace all the <li> and </li> but leave the surrounding <ul> and </ul>.