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 7 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 8 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 9 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 10 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 11 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");
        });
      });
    }

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