get_the_excerpt( int|WP_Post $post = null ): string

Retrieves the post excerpt.

Parameters

$postint|WP_Postoptional
Post ID or WP_Post object. Default is global $post.

Default:null

Return

string Post excerpt.

Source

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

Hooks

apply_filters( ‘get_the_excerpt’, string $post_excerpt, WP_Post $post )

Filters the retrieved post excerpt.

Changelog

VersionDescription
4.5.0Introduced the $post parameter.
0.71Introduced.

User Contributed Notes

  1. Skip to note 8 content

    If this function is used outside The Loop and the post doesn’t have a custom excerpt, this function will use wp_trim_excerpt() to generate an excerpt. That function uses get_the_content(), which must be used with The Loop and will cause problems if get_the_excerpt() is being used outside The Loop. In order to avoid the issues, use setup_postdata() prior to calling get_the_excerpt() to set up the global $post object.

  2. Skip to note 9 content

    Use excerpt for HTML meta description
    [html]
    <!– 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 } ?>
    [/html]

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

  3. Skip to note 10 content

    Limit Manual Excerpt displayed on Homepage to 260 characters but truncate after the last word

    This code snippet must be placed in the theme where the_excerpt() is located. This is for modifing manually entered excerpts NOT automatic ones WordPress will grab from the content.

    It will display the first 260 characters of a manually entered excerpt but instead of ending on the 260th character it will truncate after the closest word. To change the number of characters simply change the value ‘260’ to another number. Remember to remove the_excerpt() in your theme and replace it with this:

    <?php
    $excerpt = get_the_excerpt();
    
    $excerpt = substr($excerpt, 0, 260);
    $result = substr($excerpt, 0, strrpos($excerpt, ' '));
    echo $result;
    ?> 
  4. Skip to note 11 content

    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

  5. Skip to note 12 content

    Expandable excerpt function

    first the PHP
    Add this function in function.php

    function expandable_excerpt($excerpt)
    {
    	$split = explode(" ", $excerpt); //convert string to array
    	$len = count($split); //get number of words
    	$words_to_show_first = 19; //Word to be dsiplayed first
    	if ($len > $words_to_show_first) { //check if it's longer the than first part
    
    		$firsthalf = array_slice($split, 0, $words_to_show_first);
    		$secondhalf = array_slice($split, $words_to_show_first, $len - 1);
    		$output = '<p class="event-excerpt" >';
    		$output .= implode(' ', $firsthalf) . '<span class="see-more-text">...see more</span>';
    
    		$output .= '<span class="excerpt-full hide">';
    		$output .= ' ' . implode(' ', $secondhalf);
    		$output .= '</span>';
    		$output .= '</p>';
    	} else {
    		$output = '<p class="event-excerpt">'  .   $excerpt . '</p>';
    	}
    	return $output;
    }

    Required CSS
    CSS to simply hide elements when needed
    .excerpt-full.hide {
    display: none;
    }
    .see-more-text.hide {
    display: none;
    }

    required JS
    script to add/remove css classes when needed

    const itemSeeMore = document.querySelectorAll(
      "p.event-excerpt> span.see-more-text"
    );
    if (itemSeeMore) {
      itemSeeMore.forEach((item) => {
        item.addEventListener("click", () => {
          item.classList.toggle("hide");
          item.nextElementSibling.classList.toggle("hide");
        });
      });
    }
  6. Skip to note 13 content

    Fixing when get_the_excerpt() and the_excerpt() are showing Editor content, when the Editor has been turned off on the post_type

    I ran into an issue with some legacy content in a custom post type, that originally had the content 'editor' in the register_post_type() arguments 'supports' => (..., 'editor'), but had since been removed in favor of using some custom fields to better standardize the content structure in the templates. This had the result of displaying vestigial content from the editor in the templates where get_the_excerpt() or the_excerpt() were called. The following is a filter to catch the $post_excerpt string before it gets returned, and to both check if 'editor' is turned off on the post_type, and then try to find a suitable alternative field in post_meta. It borrows code from wp_trim_excerpt() to both trim length and then add the [...] as would be expected for the get_the_excerpt() and the_excerpt() results.

    /**
     * WordPress get_the_excerpt() and the_excerpt() do not check if the 'editor' is turned off on the post_type or not,
     * so vestigial content can be displayed. This checks if the 'editor' is turned on for the $post->post_type, and tries 
     * to find a suitable alternative field if applicable.
     */
    function wpdocs_custom_excerpt( $post_excerpt, $post ) {
    	$supports_editor_content = post_type_supports( $post->post_type, 'editor' );
    
    	if ( ! $supports_editor_content ) {
    		$post_meta = get_post_meta( $post->ID );
    
    		// Try to find alternative field to pull an excerpt replacement from...
    		$post_excerpt = $post_meta['overview'][0] ?? $post_meta['description'][0] ?? ''; // <-- Example: Customize this code here for your theme
    
    		if ( '' !== $post_excerpt ) {
    			// Trimming to excerpt_length via code lifted from wp_trim_excerpt...
    			// Ref: https://developer.wordpress.org/reference/functions/wp_trim_excerpt/
    			$excerpt_length = (int) _x( '55', 'excerpt_length' );
    			$excerpt_length = (int) apply_filters( 'excerpt_length', $excerpt_length );
    			$excerpt_more = apply_filters( 'excerpt_more', ' […]' );
    
    			$post_excerpt = wp_trim_words( $post_excerpt, $excerpt_length, $excerpt_more );
    		}
    
    	}
    
    	return is_string( $post_excerpt ) ? $post_excerpt : '';
    }
    add_filter( 'get_the_excerpt', 'wpdocs_custom_excerpt', 10, 2 );

You must log in before being able to contribute a note or feedback.