Jean-Francois Paradis

Web Developer + Architect

EE Excerpt Plugin

| Comments

WordPress has several easy-to-use features that are hard to leave behind when you move to other platforms. One of them is the function the_excerpt().

The Codex defines the_excerpt() as follows: “displays an excerpt of the current post with an ellipsis […] at the end; all HTML tags are stripped from the text; and only the first 55 words are kept.”

In the actual WordPress source code, we see that the words are defined as the characters separated by a specific non-word characters (\n, \r, \t, and space).

However, for some East Asian languages, the words are defined as the individual characters. Therefore, our sk_excerpt plugin gives the flexibility to chose either word or character count. When you use the character count, it will try not to cut inside a word. Here are some use cases:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Use the default 200 characters, cutting only between words.
{exp:sk_excerpt chars='true'}{content}{/exp:sk_excerpt}

// Use 500 characters.
{exp:sk_excerpt chars='500'}{content}{/exp:sk_excerpt}

// Use the default 50 words.
{exp:sk_excerpt words='true'}{content}{/exp:sk_excerpt}

// Use 55 words.
{exp:sk_excerpt words='55'}{content}{/exp:sk_excerpt}

// Also change the ellipsis from ' [...]' to '...'
{exp:sk_excerpt words='55' more='...'}{content}{/exp:sk_excerpt}

Examples:

1
2
3
4
5
{exp:sk_excerpt words='3'}After you login to the store, you can enter your bank detail under Accounts->Bank information.{/exp:sk_excerpt}
{exp:sk_excerpt words='3'}在您登录到商店里,您可以在银行信息栏中输入相应的内容。{/exp:sk_excerpt}

{exp:sk_excerpt chars='7'}After you login to the store, you can enter your bank detail under Accounts->Bank information.{/exp:sk_excerpt}
{exp:sk_excerpt chars='7'}在您登录到商店里,您可以在银行信息栏中输入相应的内容。{/exp:sk_excerpt}

Output:

1
2
3
4
After you login []
在您登录到商店里,您可以在银行信息栏中输入相应的内容。
After you []
在您登录到商店 []

However, for most layouts, I find that we get a more consistent look between languages when we think in terms of space used on the screen, so the algorithm switches to characters mode when the words have more than 10 characters on average.

Examples:

1
2
3
4
5
6
7
{exp:sk_excerpt words='3' chars='7'}
After you login to the store, you can enter your bank detail under Accounts->Bank information.
{/exp:sk_excerpt}

{exp:sk_excerpt words='3' chars='7'}
在您登录到商店里,您可以在银行信息栏中输入相应的内容。
{/exp:sk_excerpt}

Output:

1
2
Ater you login []
在您登录到商店 []

By providing two measures (words and chars) which can be adjusted individually, we can accommodate the fact that the Chinese words are slightly wider.

You can get the code on GitHub or through Devot-ee.

Comments