You are reading_

Custom field is among the most useful features in WordPress that makes it easier to be developed as a CMS. Most of the current usage of custom field are focusing to output certain info or images in a blog entry.
Have you thought of using it along with conditional statement? Here’s how to implement it
In this tutorial, I’ll be using one simple example.
Normally, blog owners will display all their post in full content (the_content) and some of them preferred to only show the excerpt (the_excerpt). But here, we will create a conditional statement to allow the blog owner to choose to display full content or the excerpt every time they wanted to publish a new post - right from the Write Dashboard.
Now, you questioned me - why don’t just use the <!--more--> tag instead? Well, the advantage of using the_excerpt is that you can have an Optional Excerpt- without having to alter your original post. And it doesn’t cut off your RSS feed too
Anyhow, this is just an example for the purpose of the tutorial, you can apply this for other purposes as well.
Open your index.php and locate the loop. Put this fragment of code inside the loop, or to be precise, where you want the content to appear.
<?php $view = get_post_meta($post->ID, 'content', $single = true); ?>
<?php if($view === excerpt ) { ?>
<?php the_excerpt(); ?>
<?php } else { ?>
<?php the_content(); ?>
<?php } ?>
Or if you are editing an existing index.php in your theme folder, you can just replaceĀ <?php the_content(); ?> with above code.
So, what does the above code means? First, we create a variable $view to get the value from a custom field with a key named ‘content’ (explained below). Then the value in this variable is compared inside the if..else conditional statement. If the value from the custom field is ‘excerpt’, WordPress will only show the excerpt of the post, else if the custom field is empty (or misspelled), WordPress will always show the full content. Simple enough?
So when you’re writing a new post, if you decide to use the excerpt rather than the full content, scroll down and add a new custom field. Put the key as ‘content’ and the value as ‘excerpt’ - as shown in the following image.

Then click add new custom field. Publish your post and you’re done.
The Optional Excerpt section allow you to put your own introduction paragraph about your post.

And if you decided to change your mind, you can just delete the custom field you created earlier.
Here an example screenshot. The red squared area shows the excerpt
Since you have to type it in manually, make sure you choose a simple word so you wont mispelled it.
And for some reason, I couldn’t get the_excerpt working on a WP 2.5.1, but it works fine on WP 2.5 and below. A bug perhaps?
You can use this trick to have different CSS style for any post(s) you want. This example only shows a simple one-line code between the if..else, but you can have a whole different CSS IDs and classes there. It’s gonna be cool!
Good luck ![]()
gestroud —
08 Jun 08 / #You’re a lifesaver. Gonna test it out right now.
azraai —
08 Jun 08 / #inche dafi dah faham php.
Fath —
08 Jun 08 / #Waa… kagum dengan inche dafi.
flisterz —
08 Jun 08 / #haha apehal inchek ni. basic camni dah lame tahu dah, Azraai - lebih kurg c++ je. yg advanced ah masih kurg.
redblaque —
09 Jun 08 / #ahhh dude. this can be blaaardy useful. thx for bringin it up!
bob —
09 Jun 08 / #hehe.. good good
need more 
asx —
09 Jun 08 / #Nice tutorials . Can be implemented not just to wordpress. Btw , c++ laen sket . hehe . lagipun semua language sama saja . Cuma syntax yang berbeza. =)
flisterz —
10 Jun 08 / #@redblaque: oh welcome. Never know you’re using wordpress. heh
@bob: thanks Bob! more to come
@asx: thanks. well, those if..else thing same je concept die.
Lance —
12 Jul 08 / #Thanks for the resource. This is the closest I’ve come to solving my issue but for some reason I can’t get it. How would one say?
IF custom field is present
XXXXX
ELSE
YYYYYY
I’d really appreciate any insight on this because I can’t find an example anywhere.
flisterz —
12 Jul 08 / #@Lance: well maybe you can do it like this;
< ?php if($view !== ' ') { ?>xxx
< ?php } else { ?>
yyy
< ?php } ?>
which means if custom field is not empty, xxx, else, yyy.
mudme —
27 Aug 08 / #tq very very much…
matt —
11 Sep 08 / #what if I want to put an image instead of a text excerpt. How do I do that in the Massive News wordpress theme? Much obliged for any help…