When using WordPress and managing your content, you have the ability to go about adding excerpts to WordPress posts and pages.
To add excerpts to WordPress posts is easy. Although the Excerpt box is hidden by default, you can make the box visible by expanding the
Screen Options section.
Unfortunately, adding excerpts to page isn’t available out of the box. If you have access to your WordPress theme’s
functions.php file, you can add the following line to enable excerpt support for pages.
add_post_type_support( 'page', 'excerpt' );
This will allow you to check the Excerpt box when adding or editing a page.
You can then add text of your choice within the Excerpt box that appears underneath the main content editor.
In addition, when using the
get_post() WordPress PHP function, the
post_excerpt data will be populated with the post and page’s excerpt added within the admin.
$post = get_post(1); // Replace 1 with your post or page ID print_r($post->post_excerpt); // Prints the post/page excerpt configured in the admin
Note that although there is a
get_page() function, this has been deprecated as of WordPress version 3.5.0. Therefore to get page data, you could use
get_post() passing in the ID of the page.
The alternative to using post and page excerpts is to use the
You can then pass in the post content (
$post->post_content) as an argument and the function will automatically trim your page content. By default, the content will be trimmed to 55 words, although this can be changed by passing in an integer as a second argument into the function.
The third parameter is a string to append to the trimmed content.
wp_trim_words($post->post_content, 55, '...');
This might be useful if you do not want to manually add excerpts to posts and pages in the admin, or if you don’t have access to enable excerpts from within the code/the admin area.
Note: This article is based on WordPress version 4.9.2.