Codex

Function Reference/get the excerpt

Contents

Description

Returns the post excerpt for assignment to a variable. This tag must be within The Loop.
If you only wish to print the excerpt you may prefer to use the_excerpt().

Usage

<?php $excerpt = get_the_excerpt( $deprecated ) ?>

Parameters

$deprecated is not required.

Return Values

  • If the post does not have an excerpt, it returns an empty string, with "[...]" at the end.
  • For password protected pages it returns a string, which has a default value of "There is no excerpt because this is a protected post." This text can be changed in the function definition.
  • If the post has an excerpt and is not password protected, it returns the excerpt as a string.

Examples

get_the_excerpt() can be used to retrieve and store the value in a variable, without outputting it to the page.

<?php
$my_excerpt = get_the_excerpt();
if ( $my_excerpt != '' ) {
	// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>

Use get_the_excerpt() to print an excerpt by specifying a maximium number of characters.

<?php
the_excerpt_max_charlength(140);

function the_excerpt_max_charlength($charlength) {
	$excerpt = get_the_excerpt();
	$charlength++;

	if ( mb_strlen( $excerpt ) > $charlength ) {
		$subex = mb_substr( $excerpt, 0, $charlength - 5 );
		$exwords = explode( ' ', $subex );
		$excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
		if ( $excut < 0 ) {
			echo mb_substr( $subex, 0, $excut );
		} else {
			echo $subex;
		}
		echo '[...]';
	} else {
		echo $excerpt;
	}
}
?>

Source File

get_the_excerpt() is located in wp-includes/post-template.php.

Related

the_excerpt()

See also index of Function Reference and index of Template Tags.