1. Home
  2. Computing & Technology
  3. PHP / MySQL

Choose Full Post or Excerpt
Using custom fields choose if you will display the full post or a post excerpt

By Angela Bradley, About.com

I have a personal blog, and two work blogs. I pull posts from my work blogs into my personal blog using FeedWordPress. I wanted these posts included in my personal blog, because they are part of what I do every day... BUT I didn't want them totally cluttering up the page that also has my personal posts. What I wanted to do is show a short excerpt from the work blogs, but show the full text for my personal posts. I was able to do that with this little bit of code based on code I found at WPRecipes:
<?php
$customField = get_post_custom_values("full");
if (isset($customField[0])) {
//Custom field is set, display a full post
the_content();
} else {
// No custom field set, let's display an excerpt
the_excerpt();
?>
[<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">full article</a>]
<br />
<?php
}
?>

So what is this code actually doing? This code checks for a custom field on the post. In the case of the code above, it is looking for the custom field "full" but this can be changed to anything. Also, the value for "full" can be anything, it is simply checking that it exist. If the field "full" exists, then the full post is shown. If it does not exist, then only an excerpt from the post is shown.

What is a custom field? When you make a new post in WordPress, or when you import a post, scroll down below where you add your post text, and you will find a box named "Custom Fields." You can add a field to a post here when you make or edit a post. In the case of my blog, if I make a personal post I simply add the field "full" and set the value to "yes" (although what I set the value to is irrelevant.)

And where does this code go? The code above can replace the normal code that calls a post in several files of your blog. All of these files will be located in /wp-content/themes/yourtheme. The names of the files can vary, but try looking for some of these common file names: index.php, archive.php, archives.php, category.php, search.php

You are replacing the code that looks like this:

<?php the_content('[...]'); ?>
Have another way to do this? Share your code here, or view other's solutions!
More PHP / MySQL Quick Tips
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Tools & Resoruces
  5. Blogs
  6. Choose the_content and the_excerpt in WordPress - Show some excerpts and some full posts in WordPress

©2009 About.com, a part of The New York Times Company.

All rights reserved.