get_the_excerpt() - WordPress 函数参考中文文档

返回介绍

get_the_excerpt()

发布于 2017-09-11 字数3915 浏览 896 评论 0

get_the_excerpt( int|WP_Post $post = null )

Retrieves the post excerpt.


描述


参数

$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 );
}

更新日志

Version 描述
4.5.0 Introduced the $post parameter.
0.71 Introduced.

相关函数

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 描述

    
    <!-- Use Post excerpt for meta 描述.  -->
    <?php if ( is_single() ) { ?>
    <meta name="描述" 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
    ?>
    

上一篇:get_the_date()

下一篇:get_the_generator()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!