Timber Logo

You are reading the documentation for Timber v2.x. Switch to the documentation for Timber v1.x.

Timber\​PostExcerpt

It’s designed to be used through the Timber\Post::excerpt() method. The public methods of this class all return the object itself, which means that this is a chainable object. This means that you could change the output of the excerpt by adding more methods. But you can also pass in your arguments to the object constructor or to Timber\Post::excerpt().

By default, the excerpt will

  • have a length of 50 words, which will be forced, even if a longer excerpt is set on the post.
  • be stripped of all HTML tags.
  • have an ellipsis (…) as the end of the text.
  • have a "Read More" link appended, if there’s more to read in the post content.

One thing to note: If the excerpt already contains all of the text that can also be found in the post’s content, then the read more link as well as the string to use as the end will not be added.

This class will also handle cases where you use the <!-- more --> tag inside your post content. You can also change the text used for the read more link by adding your desired text to the <!-- more --> tag. Here’s an example: <!-- more Start your journey -->.

You can change the defaults that are used for excerpts through the timber/post/excerpt/defaults filter.

Twig

{# Use default excerpt #}
<p>{{ post.excerpt }}</p>

{# Preferred method: Use hash notation to pass arguments. #}
<div>{{ post.excerpt({ words: 100, read_more: 'Keep reading' }) }}</div>

{# Change the post excerpt text only #}
<p>{{ post.excerpt.read_more('Continue Reading') }}</p>

{# Additionally restrict the length to 50 words #}
<p>{{ post.excerpt.length(50).read_more('Continue Reading') }}</p>

Overview #

Methods #

NameReturn TypeSummary/Returns
__construct()PostExcerpt constructor.
__toString()stringReturns the resulting excerpt.
chars()\Timber\PostExcerptRestricts the length of the excerpt to a certain amount of characters.
end()\Timber\PostExcerptDefines the text to end the excerpt with.
force()\Timber\PostExcerptForces excerpt lengths.
length()\Timber\PostExcerptRestricts the length of the excerpt to a certain amount of words.
read_more()\Timber\PostExcerptDefines the text to be used for the "Read More" link.
strip()\Timber\PostExcerptDefines how HTML tags should be stripped from the excerpt.

Class Methods #

__construct() #

PostExcerpt constructor.

__construct( \Timber\Post $post, array $options = [] )

NameTypeDescription
$post\Timber\PostThe post to pull the excerpt from.
$optionsarrayAn array of configuration options for generating the excerpt. Default empty.

  • $words
    int Number of words in the excerpt. Default 50.
  • $chars
    int or bool Number of characters in the excerpt. Default false (no character limit).
  • $end
    string String to append to the end of the excerpt. Default '…' (HTML ellipsis character).
  • $force
    bool Whether to shorten the excerpt to the length/word count specified, even if an editor wrote a manual excerpt longer than the set length. Default false.
  • $strip
    bool Whether to strip HTML tags. Default true.
  • $read_more
    string String for what the "Read More" text should be. Default 'Read More'.
  • $always_add_read_more
    bool Whether a read more link should be added even if the excerpt isn’t trimmed (when the excerpt isn’t shorter than the post’s content). Default false.
  • $always_add_end
    bool Whether the end string should be added even if the excerpt isn’t trimmed (when the excerpt isn’t shorter than the post’s content). Default false.

__toString() #

Returns the resulting excerpt.

Returns: string


length() #

Restricts the length of the excerpt to a certain amount of words.

length( int $length = 50 )

Returns: \Timber\PostExcerpt

NameTypeDescription
$lengthintThe maximum amount of words (not letters) for the excerpt. Default 50.

Twig

<p>{{ post.excerpt.length(50) }}</p>

chars() #

Restricts the length of the excerpt to a certain amount of characters.

chars( int|bool $char_length = false )

Returns: \Timber\PostExcerpt

NameTypeDescription
$char_lengthint or boolThe maximum amount of characters for the excerpt. Default false.

Twig

<p>{{ post.excerpt.chars(180) }}</p>

end() #

Defines the text to end the excerpt with.

end( string $end = '&hellip;' )

Returns: \Timber\PostExcerpt

NameTypeDescription
$endstringThe text for the end of the excerpt. Default .

Twig

<p>{{ post.excerpt.end('… and much more!') }}</p>

force() #

Forces excerpt lengths.

What happens if your custom post excerpt is longer than the length requested? By default, it will use the full post_excerpt. However, you can set this to true to force your excerpt to be of the desired length.

force( bool $force = true )

Returns: \Timber\PostExcerpt

NameTypeDescription
$forceboolWhether the length of the excerpt should be forced to the requested length, even if an editor wrote a manual excerpt that is longer than the set length. Default true.

Twig

<p>{{ post.excerpt.length(20).force }}</p>

read_more() #

Defines the text to be used for the "Read More" link.

Set this to false to not add a "Read More" link.

read_more( string $text = 'Read More' )

Returns: \Timber\PostExcerpt

NameTypeDescription
$textstringText for the link. Default 'Read More'.

strip() #

Defines how HTML tags should be stripped from the excerpt.

strip( bool|string $strip = true )

Returns: \Timber\PostExcerpt

NameTypeDescription
$stripbool or stringWhether or how HTML tags in the excerpt should be stripped. Use true to strip all tags, false for no stripping, or a string for a list of allowed tags (e.g. '

'). Default true.