Adding custom excerpts to Pages in WordPress

Wordpress functions
Published: by
Last updated:

Pages is one of WordPress’ main post types, but doesn’t have a customisable excerpt field. There are a couple of reasons why developers might a custom excerpt for WordPress pages.

This is an ongoing post. Please suggest corrections, explanations, etc. in the comment section at the bottom of this page.

With WordPress, excerpts are commonly used as a summary for Post and Page post types (Posts and Pages). They are commonly used on paginated, archive and search pages to interest readers and make them want to read further. It also helps to highlight key points of the content as a title is sometimes not enough to get your audience’s attention.

By default, WordPress limits excerpts to the first 55 words of the Post/Page. This might be enough for most WordPress users, but can be annoying if the user wants to have their own wording the content and not have sentences abruptly truncated at word 55.

To overcome this, WordPress added a custom excerpt meta box for Posts that allows exactly this, a custom excerpt for that post. If this box is left empty, then WordPress will use its default settings (see later).

WordPress custom excerpt field
Side-by-side screens of the Post (left) and a Page Edit Pages. Note that the Post Edit Screen has an excerpt meta box (red circle) and that the Page Edit Screen does not.

Page, on the other hand, does not have a custom excerpt input and that is exactly what this post is all about – adding custom excerpts to Pages in WordPress.

Activating custom excerpts for WordPress Pages

Now that we have gone over a reason why developers might need custom excerpts for Pages, let’s see how we can get this done.

At the time of updating this post, there is only one way to activate custom excerpts for Pages and it, unfortunately, involves adding WordPress code as a plugin or to your (child) theme’s functions.php file.

Using the WordPress add_post_type_support() function

WordPress has a function called add_post_type_support(). According to the WordPress code reference, the aim of the add_post_type_support() function is to register certain features for a certain post type. It goes further by saying that all core WordPress features are directly associated with a functional area of the edit screen, such as the editor or a meta box. Apart from the ‘excerpt’, these features also include ‘title’, ‘editor’, ‘comments’, ‘revisions’, ‘trackbacks’, ‘author’, ‘page-attributes’, ‘thumbnail’, ‘custom-fields’ and ‘post-formats’. The default post types in WordPress are ‘post’, ‘page’, ‘attachment’, ‘revision’ and ‘nav_memu_item’.

In its simplest form, the add_post_type_support() function will need two arguments/parameters, the post type and the feature that needs to be added. To activate custom excerpts for Pages, the post type is page and the feature is excerpt:

// Adding the excerpt feature or the Post post type
add_post_type_support( 'page', 'excerpt' );

Once the theme or plugin code is updated and saved, new and existing Pages will show an excerpt meta box.

Excerpt for Pages are activated

Using WordPress Page excerpts

Adding custom excerpts to Pages will often be enough for WordPress to start using them automatically in the default areas of the rest of the site. For WordPress websites that make frequent use of Pages, developers might also want to add Page excerpts to additional areas.

For Posts, the excerpt can be called by one of two WordPress functions: the_excerpt() and get_the_excerpt(). After excerpts for Pages has been activated, their excerpts are called with the same two functions.

By looking at the WordPress code reference, the major difference between these two functions is that get_the_excerpt() collects the raw excerpt from the WordPress database and the_excerpt() gives a filtered version of get_the_excerpt() – most noticeably adding paragraph tags to the excerpt. The the_excerpt() function also echoes the excerpt whereas get_the_excerpt() returns it.

As mentioned before, if the excerpt meta box is left empty and the get_the_excerpt() or the the_excerpt() function is called, then WordPress will still generate its own excerpt based on the first text in the content section. By adding text to the excerpt meta box, WordPress will use that one instead.

To prevent WordPress from generating its own excerpt, the has_excerpt() function can be used. This function will return a true or false value depending on whether the Page (or Post) has an excerpt or not:

if ( ! has_excerpt() ) {
    echo '';
} else { 
    the_excerpt();
}
About the author
Renier busies himself with improving his English writing, creative web design and his websites, photoshopping, micro-electronics, multiple genres of music, superhero movies and badass series.
Behind the Scenes is a free, informative website. If you find value in any of our content, please consider making a donation to our cause.
Donate via PayPal

Save, share & Disqus

Use the buttons below, on the left or the bottom of this page to share this post. Your comment is important, but don't be a knob. Keep it constructive and polite.

Comment via Disqus

Disqus is a worldwide comment hosting service for web sites and online communities. This secure platform ensures a pleasant commenting environment which is manageable from one account. Use the Login button to sign up.

More website development related posts

Improve your skills through Skillshare – get first 2 months for free
Improve your skills through Skillshare – get first 2 months for free
19 October 2020
Ad: Join the Skillshare online learning community with videos on programming, website development, art, designs and many more. Try Skillshare for free for two months now! More…