Function Reference/get the excerpt

来自WordPress中文文档
跳转至: 导航搜索

wordpress.org.cn

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 onlyincludecodewp-includes/post-template.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn.

Related

the_excerpt()

includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude