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 #
This class implements Stringable
Methods #
Name | Return Type | Summary/Returns |
---|---|---|
__construct() | PostExcerpt constructor. | |
__toString() | string | Returns the resulting excerpt. |
chars() | \Timber\PostExcerpt | Restricts the length of the excerpt to a certain amount of characters. |
end() | \Timber\PostExcerpt | Defines the text to end the excerpt with. |
force() | \Timber\PostExcerpt | Forces excerpt lengths. |
length() | \Timber\PostExcerpt | Restricts the length of the excerpt to a certain amount of words. |
read_more() | \Timber\PostExcerpt | Defines the text to be used for the "Read More" link. |
strip() | \Timber\PostExcerpt | Defines how HTML tags should be stripped from the excerpt. |
Class Methods #
__construct() #
PostExcerpt constructor.
__construct( \Timber\Post $post, array $options = [] )
Name | Type | Description |
---|---|---|
$post | \Timber\Post | The post to pull the excerpt from. |
$options | array | An array of configuration options for generating the excerpt. Default empty.
|
__toString() #
Returns the resulting excerpt.
Returns: string
chars() #
Restricts the length of the excerpt to a certain amount of characters.
chars( int|bool $char_length = false )
Returns: \Timber\PostExcerpt
Name | Type | Description |
---|---|---|
$char_length | int or bool | The 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 = '…' )
Returns: \Timber\PostExcerpt
Name | Type | Description |
---|---|---|
$end | string | The 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
Name | Type | Description |
---|---|---|
$force | bool | Whether 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>
length() #
Restricts the length of the excerpt to a certain amount of words.
length( int $length = 50 )
Returns: \Timber\PostExcerpt
Name | Type | Description |
---|---|---|
$length | int | The maximum amount of words (not letters) for the excerpt. Default 50 . |
Twig
<p>{{ post.excerpt.length(50) }}</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|bool $text = 'Read More' )
Returns: \Timber\PostExcerpt
Name | Type | Description |
---|---|---|
$text | string or bool | Text 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
Name | Type | Description |
---|---|---|
$strip | bool or string | Whether 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. ' |