testbox

Banner Custom Field

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

Introduction : the example

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.

The code

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?

Inside the Dashboard

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.

Custom Field

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.

excerpt

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

.. but you have to know

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 ;)

Share/save this: Delicious | StumbleUpon

14 comments

  1. gestroud - 08 Jun 2008

    You’re a lifesaver. Gonna test it out right now.

  2. inche dafi dah faham php.

  3. Waa… kagum dengan inche dafi. :P

  4. flisterz - 08 Jun 2008

    haha apehal inchek ni. basic camni dah lame tahu dah, Azraai – lebih kurg c++ je. yg advanced ah masih kurg.

  5. ahhh dude. this can be blaaardy useful. thx for bringin it up!

  6. hehe.. good good :) need more ;)

  7. Nice tutorials . Can be implemented not just to wordpress. Btw , c++ laen sket . hehe . lagipun semua language sama saja . Cuma syntax yang berbeza. =)

  8. flisterz - 10 Jun 2008

    @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.

  9. 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.

  10. flisterz - 12 Jul 2008

    @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.

  11. tq very very much…

  12. 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…

  13. Hey, I really enjoy your blog. I have a blog too in a totally unrelated field (Online Stock Trading) but I like to check in here on a regular basis, just to see what’s going on and it’s always interesting to say the least. It’s always entertaining what people have to say.

  14. Thank you! This is exactly what I have been looking for. The general logic is very powerful and can be used in many other situations.

    This should be in the official WordPress codex.


Leave a comment