Hand-written Includes in-depth notes
Excerpt content_excerpt
Components and page templates
Trim the excerpt text of a content item and fall back to the body when no excerpt exists; HTML is stripped.
content_excerpt($item, $length = 100)
Pages Home List pages Detail pages
How do I read these repository paths?
All paths are relative to the site root. Open core/tpl_functions.php at line 401 to see the definition of content_excerpt(). The "core method" is what the tag actually forwards to: a template writes content_list(...) and the core runs Tpl::contentList(...).
1. Fill in parameters, get the code Edit any field and the result below updates instantly; press Copy and paste it into a template. It also works without JavaScript: press Generate and the server recomputes it.
click the title to collapse / expand
Now generating: content_excerpt
This is the real rendering of the page (real header, container, this tag output, real footer). It reloads when you change a parameter.
Refreshing…
This is the server-computed default; it updates as you edit any parameter and the preview on the right is re-rendered too. It also works without JavaScript: fill the form and press Generate.
2. Full parameter table 2 parameters, one per row; scroll sideways on narrow screens.
click the title to collapse / expand
| Parameter | Type | Default | Description |
|---|---|---|---|
item |
array |
required / as needed | Content row; prefers summary, else trims the body. |
length |
int |
100 | Maximum characters (UTF-8 characters, not bytes); 0 means no truncation. |
3. Examples Every example can be copied as-is; the later ones are more advanced.
click the title to collapse / expand
Minimal
The default is 100 characters, and an empty summary falls back to the body with all tags removed.
<p class="ry-excerpt"><?= e(content_excerpt($item)) ?></p>
Fixed length inside a card
Every card uses 80 characters, and a CSS line cap keeps short and long summaries aligned.
<a class="ry-card" href="<?= e(content_url($item)) ?>">
<h3><?= e(field($item, 'title')) ?></h3>
<p class="ry-excerpt"><?= e(content_excerpt($item, 80)) ?></p>
</a>
Check, then print the block
Checking first keeps an empty summary block off a row that has neither a summary nor a body.
<?php $ryExcerpt = trim((string)content_excerpt($item, 120)); ?>
<?php if ($ryExcerpt !== ''): ?>
<section class="ry-summary">
<p><?= e($ryExcerpt) ?></p>
</section>
<?php endif; ?>
4. What it does / when to use it
Use it for the short description on a list card, the snippet line in search results, and the lead paragraph in a detail page sidebar. It prefers the summary an editor filled in and falls back to the body, stripping scripts, styles, and every tag along the way, collapsing line breaks into single spaces, and appending an ellipsis when it really truncates. Do not use it when the markup inside the body, such as bold text, links, or paragraphs, must survive, and do not rely on it alone where a complete sentence matters.
5. Which pages it belongs on This is the question clients actually ask: "I am building the home page / a list page / a detail page — which tags do I use?"
click the title to collapse / expand
| Page | What that page is | What this tag does there |
|---|---|---|
| Home | Home page: hero, category blocks, latest content |
On the home page it usually sits in the hero, a category block, or a latest/featured section. You use it to: Use it for the short description on a list card, the snippet line in search results, and the lead paragraph in a detail |
| List pages | Category list pages: listing, filters, pagination |
On a list page it lives in the list container or right next to the pagination bar. You use it to: Use it for the short description on a list card, the snippet line in search results, and the lead paragraph in a detail |
| Detail pages | Content detail pages: body, specs, related items |
On a detail page it goes in the body area or in the related-items sidebar. You use it to: Use it for the short description on a list card, the snippet line in search results, and the lead paragraph in a detail |
Clicking a page name jumps to every tag for that page in the reference (the "By page" filter there). This page always shows everything about the tag, whatever that filter is set to.
6. How to use it (step by step)
- Set the length first. The default is 100 characters; use 60 to 80 for a tight card and 120 to 200 for a full lead paragraph. The limit counts characters, so tune the number when the grid changes.
- The call is just content_excerpt($item, 80). It reads the summary and falls back to the body, so the template never has to decide which column to use.
- Print it inside your own container class and cap the visible lines with CSS such as line-clamp, so one unusually long summary cannot break the card.
- The same character count carries very different amounts of meaning in Chinese and English, so give the English template a larger budget or pass a separate length per language to keep both card layouts looking alike.
- Let the tag add the ellipsis. Do not append three dots in the template, or a truncated string ends with two of them. It also returns an empty string when there is no text, so test the value before printing.
7. Common pitfalls
- Letting the body stand in for a summary. When no summary is filled in, the tag extracts from the body, and a long article dumps a wall of text onto a list page. Pass a shorter length in list templates and keep the summary field in use.
- Expecting markup to survive. The extraction removes every tag and flattens line breaks, so bold text, links, and paragraphs disappear. Find another approach when a list needs that emphasis.
- Accepting a hard cut mid-sentence. The string stops at the Nth character, which can split a word or even a punctuation mark, so for a prominent spot such as a hero block ask an editor for a short hand-written summary.
- Treating 0 as the shortest option. A length of 0 or a negative value means no truncation and dumps the whole cleaned body onto the page; leaving the argument out is what gives you the default 100.
Note from the reference table The example's excerpt() does not exist in templates - use content_excerpt(). Only truncated text gets a trailing ellipsis; HTML tags, scripts and extra whitespace are stripped.
excerpt() does not exist in templates - use content_excerpt(). Only truncated text gets a trailing ellipsis; HTML tags, scripts and extra whitespace are stripped.8. Real rendered output This section was rendered once on the server with this site real data; it is not a mock-up.
click the title to collapse / expand
The summary truncated to 60 characters (HTML stripped).
Loading the code…
9. Where it already appears in your templates Grouped by components / page templates / template packs; the data comes from a script scan.
click the title to collapse / expand
Components 72 files / 77 calls 89% of tag calls live in components
-
templates/_shared/components/detail-content-advantage.php×2 lines 31, 297 -
templates/_shared/components/home-faq-highlight.php×2 lines 206, 218 -
templates/_shared/components/home-news.php×2 lines 64, 87 -
templates/_shared/components/list-large-card.php×2 lines 305, 341 -
templates/_shared/components/list-magazine.php×2 lines 340, 381 -
templates/_shared/components/case-card.php×1 lines 34 -
templates/_shared/components/detail-content-case-metrics.php×1 lines 34 -
templates/_shared/components/detail-content-case-story.php×1 lines 140
Page templates 2 files / 2 calls
-
templates/_shared/feedback.php×1 lines 37 -
templates/_shared/page.php×1 lines 26
Click "See this code + preview": the left shows the real code around that call site (±12 lines, call line highlighted) and the right renders a real page preview with the current parameters.
Common combinations (generated from your real templates)
- The page template hosting this tag is feedback
templates/_shared/feedback.php
templates/_shared/feedback.php