get_the_excerpt()获取当前文章的摘要

目录

描述

Returns the excerpt of the post. This is either a user-supplied excerpt, that is returned unchanged, or an automatically generated word-counted trimmed-down version of the full post content.

The auto-generated excerpts have all shortcodes and tags removed which means it is just an unformatted string that would not see any line-breaks in any form of output, since the actual line-breaks in the raw text are also removed.

When called through the_excerpt() this implies that the auto-gen excerpts are just raw text with <p></p> tags wrapped around it, basically.

Auto-gen excerpts have a "more" tag appended to them. This tag is '[…]' by default. It can be changed through the excerpt_more filter.

The default word length is 55 words. It can be changed through the excerpt_length filter.

get_the_excerpt() 描述

用法

 <?php $excerpt get_the_excerpt$deprecated ?> 

This function must be used within The Loop.

get_the_excerpt() 用法

参数

$deprecated is not required.

get_the_excerpt() 参数

返回值

  • Returns an existing excerpt unchanged (by default) but applies get_the_excerpt filters to it.
  • Returns a trimmed-down version of the full post content as described, additionally applying wp_trim_excerpt filters to it. 'wp_trim_excerpt' is chained from 'get_the_excerpt', meaning that your own get_the_excerpt filter will get called before or after the trim depending on the priority of your filter. If called before, your $text parameter will be an empty string if there is no user supplied excerpt, whereas it will be the trimmed-down version of the full post if called after. In the latter case it might be useful or more meaningful to use 'wp_trim_excerpt' since it has a second parameter giving you the untrimmed input to the wp_trim_excerpt function, meaning this second parameter is simply the raw $post->post_excerpt.
  • 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.

get_the_excerpt() 返回值

示例

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?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.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?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;
	}
}
?>

get_the_excerpt() 示例

历史

添加于 版本: 0.71

get_the_excerpt() 历史

源文件

get_the_excerpt() 函数的代码位于 wp-includes/post-template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve the post excerpt.
 *
 * @since 0.71
 *
 * @param mixed $deprecated Not used.
 * @return string
 */
function get_the_excerpt( $deprecated = '' ) {
	if ( !empty( $deprecated ) )
		_deprecated_argument( __FUNCTION__, '2.3' );

	$post = get_post();
	if ( empty( $post ) ) {
		return '';
	}

	if ( post_password_required() ) {
		return __( 'There is no excerpt because this is a protected post.' );
	}

	/**
	 * Filter the retrieved post excerpt.
	 *
	 * @since 1.2.0
	 *
	 * @param string $post_excerpt The post excerpt.
	 */
	return apply_filters( 'get_the_excerpt', $post->post_excerpt );
}

get_the_excerpt() 源文件

相关