返回介绍

get_the_excerpt()

发布于 2017-09-11 00:34:33 字数 3934 浏览 1003 评论 0 收藏 0

get_the_excerpt( int|WP_Post $post = null )

Retrieves the post excerpt.


description


参数

$post

(int|WP_Post) (Optional) Post ID or WP_Post object. Default is global $post.

Default value: null


返回值

(string) Post excerpt.


源代码

File: wp-includes/post-template.php

function get_the_excerpt( $post = null ) {
	if ( is_bool( $post ) ) {
		_deprecated_argument( __FUNCTION__, '2.3.0' );
	}

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

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

	/**
	 * Filters the retrieved post excerpt.
	 *
	 * @since 1.2.0
	 * @since 4.5.0 Introduced the `$post` parameter.
	 *
	 * @param string $post_excerpt The post excerpt.
	 * @param WP_Post $post Post object.
	 */
	return apply_filters( 'get_the_excerpt', $post->post_excerpt, $post );
}

更新日志

Versiondescription
4.5.0Introduced the $post parameter.
0.71Introduced.

相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/functions.php: _deprecated_argument()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post-template.php: post_password_required()
  • wp-includes/post-template.php: get_the_excerpt
  • wp-includes/post.php: get_post()
  • Show 1 more use Hide more uses

Used By

  • wp-includes/embed.php: the_excerpt_embed()
  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::column_title()
  • wp-includes/feed.php: the_excerpt_rss()
  • wp-includes/post-template.php: the_excerpt()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by hearvox

    Use excerpt for HTML meta description

    
    <!-- Use Post excerpt for meta description.  -->
    <?php if ( is_single() ) { ?>
    <meta name="description" content="<?php echo wp_strip_all_tags( get_the_excerpt(), true ); ?>" />
    <?php } ?>
    

    (Setting the second parameter of wp_strip_all_tags to true removes left over line breaks and whitespace chars.)

  2. Use

    has_excerpt()

    to prevent a Notice: Undefined offset: -1 in post-template.php

    $excerpt = '';
    if (has_excerpt()) {
        $excerpt = wp_strip_all_tags(get_the_excerpt());
    }

    Otherwise, you could get an Undefined offset -1 warning, do not try to get_excerpt directly to check whether with isset or NULL, you’ll get an undefined offset -1 back

    Example
    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
    ?>
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文