
Do you use excerpts or full posts on your blog excerpts? Personnally, I think they’re both good. The only problem is that you can’t, by default, use both excerpts and full posts on your blog homepage. Here’s a code to do it.

Do you use excerpts or full posts on your blog excerpts? Personnally, I think they’re both good. The only problem is that you can’t, by default, use both excerpts and full posts on your blog homepage. Here’s a code to do it.
On your index.php file, replace your current loop by this one:
<?php if (have_posts()) :
while (have_posts()) : the_post();
$customField = get_post_custom_values("full");
if (isset($customField[0])) {
//Custom field is set, display a full post
the_title();
the_content();
} else {
// No custom field set, let's display an excerpt
the_title();
the_excerpt();
endwhile;
endif;
?>
On the code above, the default is excerpts. When you want to show a full post on your blog homepage, simply edit the post and create a custom field named full and give it any value.
30 Responses
I have always been a sucker for conditional tags in code because I love choices. This code could very easily just be turned around to set the default to the full article.
You could also just set your code to the full article and then just use the ‘more-link’ to show excerpts where you wanted them. There is of course no other way around using this method.
Clever little snippet of code, thanks.
@Lyndi: I agree with you, unless you definitely need it, you should avoid hard coding to always keep the choice.
Greatr idea with the “more” link! I’m sure that can be done quite easily.
I use an excerpt post in another site of mine. I wonder to create an excerpt code with limited text we want. Can we add additional code for number of text we want to show?
@Evyta SEO: Good idea, I’ll think about it!
I second using more link to control full/short.
It makes sense and looks way better then showing auto-generated excerpts.
Nice bro. Just i need that.
Wow! I never thought of this. Custom fields are really powerful. I really have to start harnessing their true potential, lol. Thanks for this one.
Thanks so much, this is EXACTLY what I was looking for
Exactly what i was searching for! But doesn’t work with my theme right now, so I have to take a look at the code. Anyway, thanks a lot!
My theme appears to be incompatible with this (works when I switch out themes, but not with the one I typically use). Any thoughts as to what might be required of a theme for this to work?
I even backed up my original index.php and then creating a brand new index.php file with only the above code. I still get a blank screen.
Scratch that…it’s not working in any themes. I’m stumped. :/
I’ve verified your code, and eliminated mine. Not sure what I’m missing.
Ya this doesn’t work for me either, I just get a
Parse error: syntax error, unexpected T_ENDWHILE
Custom fields are always an good option. In this case, I don’t know what is the advantage of the field when I can directly set an “more”-tag in the post?
Shouldn’t also archive.php be modified. index.php is only working for main page.
Currently i use the more tag in any article.
the tip of rennrad sounds good, but it didnt work for me.
Thanks for this tip.
In general, I wonder why Wordpress is not created with heaps of options for all this sort of stuff. So far I have had to spend many days/weeks modifying my theme, and although it’s not too hard, I would have thought that simple requests like choosing excerpt size, on/off, and to what posts do you require an excerpt, woould have been standard in the WP admin.
It seems that to really have a good cms, you either have to spend 50,000 bucks and have a weeks training on how to use it, or spend weeks modifying a sandbox WP theme.
Sorry for rant or whatever you call it, I just hate not having options and then having to modify the hell out of stuff. I’m just going to go back to a php/mysql cms i made myself a few years ago and wait til WP becomes user friendly. Best cms there is, but just too confining.
thanks
@james
My WP installation currently has 346 options set in database alone. I don’t think I want checkbox for each one.
There is no way to take engine that complex and that flexible and dumb it down to “options”. You either deal with that or find/pay someone who does. Nothing new in this formula.
Thanks for this I kind of reversed the code using another one of your scripts http://www.wprecipes.com/how-to-check-out-if-a-post-has-an-excerpt so that it checks for an excerpt, if none exist it shows the first 20 characters of the post.
post_excerpt)) {//This post have an excerpt, let's display it
the_excerpt();
} else {
// This post have no excerpt
the_content_rss('', TRUE, '', 20);
} ?>
I’ve been trying the code $query = “SELECT * FROM wp_posts ORDER BY post_date DESC LIMIT 1″;
$returns = mysql_query($query);
$result = mysql_fetch_row($returns);
$post_title = $result[5];
$post_excerpt = $result[7];
echo $post_title;
to display articles in another website with the correct connections but it won’t display the full article on the title
Any ides please?
andrew
Hi, Just like to tell you that this piece of info is one quick to the point, no nonsense, workable and effective way to as fast as possible. It worked for me and thank you for the effort. Keep up the good work.
I am following your blog, for a long time. Because, your blog is including, useful and necessary information. Your blog is in my browser’ s bookmarks.
Good jobs.
Trackbacks: