Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Talk:Function Reference/the excerpt

Text reads "HTML tags and graphics are stripped from the excerpt's content." ... and nobody has complained loud enough? Ok, images can be considered merely aesthetic, but /what/ do you gain by stripping URLs?! This seems merely perverse ... like special treatment for "cockney". --bentrem 05:30, 27 December 2007 (UTC)


I'd really like to see a use of the Excerpt used within the loop so people will know where it goes. Also expanding info on whether or not it will show the actual "excerpt" as written in the "excerpt" field or only the first 120 words of the test. Add to that any CSS classes that can be set in the excerpt so it can be modified on the category and index.php pages. Would this info or links to this kind of info be appropriate to add here?

Lorelle 02:37, 22 Jan 2005 (GMT)
Not a bad idea, but how much expansion? In the pages I've edited, I've gone for short but sweet descriptions. Are there any thought out there on how much info is too much? And why aren't you helping out on the Codex and turning your ideas into reality!? After all, we're just fellow users here... :) -Kaf 22:00, 23 Jan 2005 (GMT)

I've been editing these pages by the gross (and they've needed it), learning about the program as I go. I certainly can't write about things I don't know anything about, but when I do learn it, it'll show up here. I'm doing my part, it just doesn't have my name splashed all over it.

As for the expansion...enough so we understand where to put it. To say "put it inside the loop" isn't very specific. Where specifically do we put it? The rule of thumb for technical pages like these is short, concise, and redundant. Presume total beginner and give them enough to put it in context as simple and thorough as possible - even to redundancy. Does that help?

Lorelle 01:53, 25 Jan 2005 (GMT)

Couple of more things I'd like to see on this page.

1. Are there any CSS classes or divs in the excerpt?
2. Can you include graphics or stylize the excerpt so it is different from what the "summary" is? (In other words, using the first 120 words of the post would look like the beginning of the post, but if you can stylize the excerpt, can you make it look different?)
3. Are there any limits to the size of the excerpt content (120 words - 1000 characters...anything?)
4. Are there any "unusual" ways to use excerpts, like only to feature a photograph or graphic which would lead to the post, or some gimmick?
5. Could the excerpt function have any parameters to increase its function?
Lorelle 02:51, 25 Jan 2005 (GMT)


Lorelle,

To answer a few points above, well, the template tag only displays the text excerpt. That is all it does - is to retrieve whatever text is stored as "the excerpt" which is the first fewwords of the post, cleanse it using text-filters and display it wherever the tag is used. No css, or any other stuff. The size of the excerpt if a seperate excerpt is not typed in by the user is the first "N" characters, where "N" is a setting, that is hardcoded. I do not remember it off the bat now.

Keep the notes coming, they will make for better docs - I really appreciate your inputs, and we'll see if we can answer a few of these and other questions as we go along.

Carthik 06:21, 25 Jan 2005 (GMT)
Excuse me, but are you saying that there is a text filter that will strip out...what?...and only display text in the excerpt? If I put into the excerpt itself a graphic, html, css, or whatever, that would be cleansed? I edit these Codex pages from the view of the user, like me, and one of the things I want to do, which I assume others might, is have my excerpt look different from the actual page, featuring a photo from the article and having it match the look of the index and/or category pages where the excerpt would be displayed. So could you clarify this for me and help me come up with a way of adding this tidbit to the article? Thanks.
Lorelle 16:52, 2 Feb 2005 (GMT)
Yes, the_excerpt() strips all HTML (i.e. tag) elements from an excerpt or abbreviated content of a post. As I understand it, the original purpose of the_excerpt was to provide a (textual) preface, summary, or explication of a post. I imagine it's still all a majority require of it. But for those who find it limiting, it's part of the reason some of us write plugins... [I'd ignore most of this particular paragraph and read MDAWaffe below. New personal rule: do not try posting explanatory comments when you really need to go to sleep!]
Carthik, the "N" is actually 120 words (give or take, based on the method WP calculates a word) -Kaf 07:33, 4 Feb 2005 (GMT)


the_excerpt displays the excerpt. It tries to do this in two different ways. First it looks to see if an excerpt has been entered explicitly by the author (Write->Write Post->Advanced Editing->The "Excerpt" textbox above the main textbox). If such an explicit excerpt has been entered, it will display that without stripping any HTML. So you're image trick might work if you specify the image tag there. If the_excerpt does not find an explicitly written excerpt, it generates one on the fly by grabbing the first ~120 words from the HTML stripped content of the post, as Kaf mentioned.
1. You can style it however you want: <div class="excerpt"><?php the_excerpt(); ?></div>
2. I don't really understand. Are you asking whether or not you can style the excerpt differently depending on how it generates its output (i.e. depending on whether it uses the first or second methods described above)? If that's the question, then no. Not without some serious hacks or writing a plugin.
3. If the_excerpt is generated on the fly, ~120 words as above. It's it's explicitly input, the database structure limits it to something like 2^16 characters (I don't know... ~10,000 words) as opposed to the_content which can be 2^32 (recently lengthened to this value which is... man half a billion words? - am I reading the SQL docs right?... UTF-8 is one byte per character right? Anyway, ridiculously off topic.)
4. I'm sure there are.
5. the_excerpt has no parameters. I doubt it ever will. The underlying function it references (get_the_excerpt) has one: $fakeit = TRUE/FALSE. TRUE is the default, and it will work in the same way I described above. FALSE means it will not try and generate an excerpt on the fly; if there's no explicit excerpt, it outputs nothing. To use this function: <?php echo get_the_excerpt(FALSE); ?> (for example). The output is not, by default, filtered through any WordPress filters, though, so it should only be used if you know that's what you want.
As for where to put it... "inside the loop" is about as specific as you can get, since it depends on where the user wants it displayed; it doesn't have to go in any particular spot. "Directly under the post title inside the loop" if the user wants it under the post title (which, I imagine is the most natural place), but you can put it anywhere (and I imagine some people might - especially if they're doing something with images similar to what you described). --MDAWaffe 08:40, 4 Feb 2005 (GMT)
3. Both content and excerpt in the database are limited to 64K (yeah, about 10,000 words); this is the reason on occasion you see someone in the support forums suggest changing the data type of post_content to MEDIUMTEXT (16MB). utf-8 uses 1-4 bytes, depending on the character/symbol. </techtalk>


Excellent explanation. Let's make sure the most critical and helpful of this information (stripping tags vs not stripping) is in the article). With information in the Excerpt that isn't stripped, then "anything" can be done with it. Wonderful.

>>>Both content and excerpt in the database are limited to 64K (yeah, about 10,000 words);<<<<

I think this is highly critical information but I'm seeing it here for the first time. Where is this mentioned? It should be in the section that gives information on creating posts. Did I miss it? I'm notorious for excessive babble. So this is a MySQL limit...hmmmm.

Thank you so much. This will help with the editing.

Lorelle 15:51, 5 Feb 2005 (GMT)

<tech>As of Fenruary 2, 2005 MySQL is now using longtext (4 GB) for post_content. This seems excessive, though I suppose it's only one extra byte per post relative to mediumtext.</tech>
The content can be as many as ~half a billion words. --MDAWaffe

I see that my verbosity will not be limited now. Thanks.

Related but different, I know this isn't a place for tech support, but I followed the specific instructions in the article about replacing the the_content with the_excerpt in 1.5 and the excerpts are the only things that show up - even in the posts. I tried the if/then example and got a line error on that line. AH, took another look and the || is a signal to give a choice. This has to go. I did what everyone with less than two brain cells to rub together would do and simply copied and pasted the info directly into the code. We need to keep our examples clean enough to do that. Better to show more examples than expect us twits to know enough to recognize that the code needs editing.

Anyway, with the disappearance of the "if single post", we need to be more specific on where to put the excerpt tag within the loop related to 1.5, if there is a difference. I assume it goes in the Loop, but the Loop has changed. The first example of an if/then statement would be showing an "if excerpt - excerpt. if no excerpt, then excerpt from content or content." Clearly, better written than that....;-)

Lorelle 00:59, 6 Feb 2005 (GMT)

Copy and Paste works perfectly for me. The first example will always display the excerpt. You can combine that example with
<?php the_content(); ?>
to display both, but the_excerpt tag by itself has no knowledge of the Content and cannot display it. The || (meaning OR) in Example 2 means exactly what the description says. Are you looking at an Archive OR a Category? If yes: display excerpt. If no: display content.
It should be within The Loop as the introduction states. I don't mean to sound crass, but where in the Loop it should go entirely depends on where the user wants it displayed. --MDAWaffe 02:34, 6 Feb 2005 (GMT)

The || isn't very clear. I know it looks that way to you. I messed with it for quite a while before I looked at the code I copied and pasted and thought, hmmm, I don't remember seeing pipes in the code anywhere else. Does PHP use pipes? Remember, I'm new enough to this to be like the new users who will encounter these same bugaboos. I'd really rather see two examples than the use of the pipes. Think of all the forum questions for help this will generate if they do the same thing I did.

To FINALLY get the excerpts to work, I searched on google and found the reference from the old wiki which included the specific loop for excerpt or post excerpt:

<?php
if ($single) {
the_content();
}
else {
the_excerpt();
}
?>

Familiar? That worked like a charm. the $single made all the difference. The other code (minus the pipes) brought only excerpts on the posts. If the $single reference shouldn't work in 1.5 or should, the codex needs to reflect that. If not, it needs to have the simpliest of this form as the leading example to avoid the problem I had.

Thanks for helping me understand this. I don't know enough about the code to verify anything except by trial and error, but I can edit and fix layout "real good". If I'm having trouble with something, you can bet there are a hundred people just like me. I appreciate the help in making this understandable to everyone.

Lorelle 05:58, 7 Feb 2005 (GMT)

In 1.5 if ( is_single() ) {...} is the preferred method for doing what you describe above. See Ryan's Description (linked to from the bottom of Theme Development). I assume, then, by "on the posts" you meant "when viewing the page for a single post". I cannot speculate why copying and pasting did not work for you; as I said it worked for me. It is possible that the pipes were translated by your computer into vertical bars (not the same character). I seem to recall hearing something about that possibility at one point. Try copying and pasting again, but deleting the pipes and retyping them.
The code you describe above may accomplish what you want, but it does not do the same thing as Example 2. Your code will only display the full content on a single-post-page. Example 2 displays the content for everything that is neither a category page nor an archive page, meaning it will display the full content on the home page (assuming that the template you are editing is the one used by WordPress for all of those sorts of pages, i.e. assuming it's index.php and that you don't have a category.php, nor an archive.php, nor a single.php).
If copying and pasting again does not work, and copying and pasting with the manual deletion and addition of pipes does, please write back. That's something we'll need to know. If it doesn't work... update... then off to the forums with you :)
As to what examples should be used: I have no idea. I think Example 2 as it is is fairly instructive. It demonstrates the is_{X} functions, logical OR operators, ifs and elses. All nice things to know what are, at least, and how to use, at best. Perhaps it is too much; I honestly don't know. Suggestions? --MDAWaffe 06:52, 7 Feb 2005 (GMT)

And now for something completely different. I think we should be using 1.5 notation over 1.2 on the Codex. Thus, $cat and $m should be replaced with is_category() and is_archive() in the examples section. Thoughts? --MDAWaffe 08:49, 4 Feb 2005 (GMT)

No reason we can't do both, and note the reason for the difference in the example descriptions. -Kaf 14:53, 4 Feb 2005 (GMT)

NEED EXAMPLES NOW

Okay, I've asked and nothing is happening on this. If I is frustrated as a user, expect others to be just as stressed. The examples herein don't list the most simple of layouts, something to the effect of "list the excerpt otherwise, and especially if in a single post, list the content". Building from there to have something like "if this is the main page, or a category, or an archive, then post the excerpt, otherwise, if a single post, post the post". Building slowly on the examples.

And then there is the shorthand version of this. I have run across things like:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

Which doesn't look like any of the examples in the article. What is this, anyway? The Loop abbreviated or something else?

We need to clean this up, show good progressive examples, show how HTML can be put inbetween each of the php codes, and show how they can be shorthanded, and then get to the advanced stuff where the category changes the look. Someone please help me on this. I weak, ignorant little spirit, desperately seeking assistance to help others help themselves.

Lorelle 23:31, 4 Mar 2005 (UTC)

I'm still seeing <br /> tags in the source of my excerpts - is this correct behaviour? --Junap 18:02, 13 Nov 2006 (GMT)

Parameters...

look at this extension: http://guff.szub.net/2005/02/26/the_excerpt-reloaded/. Ramiy 22:07, 30 April 2008 (UTC)