Pages is one of WordPress’ main content types, but doesn’t make use of excerpts. There are a couple of reasons why developers might need this field in WordPress pages. This post will show you how.
Excerpts are most of the time used as a Post summary. By default, excerpts in WordPress are only available for Posts. For most WordPress websites this will be enough as Pages are generally used for once-off content that is mostly not shown under the latest site content. Pages are, however, often included in the site’s search section, where a nice formatted (or custom) excerpt will fit in better with the rest of the site.
Activating excerpts for Pages
To activate excerpts for Pages in WordPress, the following function code needs to be added to the (child) theme’s
add_post_type_support( 'page', 'excerpt' );
This code will modify the default WordPress content type ‘Page’ to add support for excerpts. Once the
functions.php is updated, new and existing Pages will show an excerpt meta box.
If this box is left empty, WordPress will still generate its own excerpt based on the first text in the content section. By adding a custom excerpt to this section, WordPress will use that one in stead.
Using WordPress Page excerpts
Adding custom excerpts to Pages will often be enough for WordPress to automatically start using them in the default areas of rest of the site. For WordPress websites that makes frequent use of Pages, developers might want to use Page excerpts in their content.
The default PHP code for calling Page excerpts in WordPress is exactly the same as for calling excerpts for Post:
<?php echo get_the_excerpt(); ?>