WordPress at a glance

the_excerpt() WP 0.71

Displays the excerpt (quote) of the post, with [...] at the end (but this is not a link to reading the full material).

If the "excerpt" field is not specified for the post (you specify it on edit post screen), the function "will create" a quote by itself, by cutting the initial part of the post content (the first 55 words of the content will be taken). HTML tags or images will be pre-cut, so they will not be displayed and taken into account when counting.

This function must be used inside WordPress Loop.

Note: If the current post is an attachment (usually displayed in a loop of template files: attachment.php and image.php), the_excerpt() will output the text specified in the "description" (Caption) field and the text will not have [...] at the end of the construction.

the_excerpt() and the_content()

Sometimes it's better to use the_content() because it understands the tag <!--more--> specified in the post content and displays the text above this tag.

Works based on: get_the_excerpt()
✈ 1 time = 0.007916s = very slow | 50000 times = 5.61s = fast PHP 7.1.2, WP 4.7.3
Hooks from function:
Return

Nothing (null). Displays a short text.

Usage

<?php the_excerpt(); ?>

Examples

#1 Basic usage

Get a post "Quote" on archive pages (categories, tags, authors, dates):

<?php the_excerpt(); ?>

#2 Use with conditional tags

Let's check if this page is a category page, if so, replace the_content() with the_excerpt():

if ( is_category() ) {
	the_excerpt();
}
else {
	the_content();
}

#3 Changing the length of the cropped text

By default, the length of the excerpt text is 55 words, to change this parameter, you can use the excerpt_length filter. Place next code in the functions.php theme file:

add_filter( 'excerpt_length', function(){
	return 20;
} );

#4 Remove [...] construction at the end

By default, if the text is truncated, [...] added at the end. We can change [...] through the excerpt_more filter. Add this code to the functions.php theme file:

add_filter('excerpt_more', function($more) {
	return '...';
});

#5 Creation of a link "Read more..." at the end

Again, let's use excerpt_more filter. Copy and paste next code into the functions.php theme file:

add_filter( 'excerpt_more', 'new_excerpt_more' );
function new_excerpt_more( $more ){
	global $post;
	return '<a href="'. get_permalink($post) . '">Read more...</a>';
}

Code of the_excerpt: wp-includes/post-template.php VER 4.9.8

<?php
function the_excerpt() {

	/**
	 * Filters the displayed post excerpt.
	 *
	 * @since 0.71
	 *
	 * @see get_the_excerpt()
	 *
	 * @param string $post_excerpt The post excerpt.
	 */
	echo apply_filters( 'the_excerpt', get_the_excerpt() );
}

Related Functions

From tag: excerpt (fragment, snippet)

More from tag: WP Loop

More from category: Posts, pages...

More from Template Tags: Posts, Pages, ...

No comments
    Hello, !     Log In . Register