WordPress.org

Ready to get started?Download WordPress

Codex

Function Reference/get the excerpt

Contents

Description

Returns the excerpt of the current post with the "[...]" text at the end, which is not a "read more" link. This template tag must be used within The Loop. Alternatively, the_excerpt() can be used to directly display the excerpt, but it also adds opening and closing <p> tags to the string returned by get_the_excerpt().

Usage

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

Parameters

$deprecated is not required.

Return Values

  • If the post does not have an excerpt, this function applies wp_trim_excerpt to the post content and returns that generated string with "[...]" at the end. wp_trim_excerpt is applied via the get_the_excerpt filter and can be removed.
  • 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;
	}
}
?>


Change Log

Since: 0.71

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.