Default Custom Fields

By Alex on June 1 2008 | Listed under Tutorials, WordPress | 18 Comments

As one of my previous tutorials on creating a dynamic slideshow from your posts showed, there are a lot of useful and interesting uses for Custom Fields. One use that I make of custom fields is to pull an image from a post and display it on my homepage, however I don’t always need something different for each post. This is where this method is a real time saver!

Of course, you could just add the same custom field for every post where you want the ‘default’ used, but why bother when we can make it easier for ourselves? The first example code below will show a basic method that can be adapted to your needs, and then the second example will develop that to demonstrate how I achieve my effect with images.

The Basic Code

The basic method will check if the desired custom field ‘key’ has no defined ‘value’, or if it is absent. If it is missing, then the default text is shown, otherwise show the ‘value’.

1
2
3
4
5
6
7
8
<?php
$var = get_post_meta($post->ID, 'My Custom Key', true);
if ($var == '')
{ 
echo "<p>I'm the default text!</p>";
} else { 
echo "<p>" . $var . "</p>"; 
} ?>

Simple!

Default Image Display

If you take a look at my WordPress category page you will see that some of the images for the posts are the same. I achieved this by extending the above code to include a default image if I didn’t need an image to specifically demonstrate the post.

1
2
3
4
<?php
$thumb = get_post_meta($post->ID, 'Homepage Image', true);
if ($thumb == '')
{ ?>

First of all we store the ‘value’ defined for the ‘key’ Homepage Image for the current post in a variable called $thumb. We then check to see if $thumb is either blank, or if there is no Homepage Image ‘key’ at all. If either of these is the case, then we do the following:

5
6
7
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<img src="http://domain.com/images/defaultimage.jpg" alt="<?php the_title(); ?>"/>
</a>

This will output an image that is on my server each time there is no custom field available.

8
9
10
11
12
<?php } else { ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<img src="<?php echo $thumb; ?>" alt="<?php the_title(); ?>"/>
</a>
<?php } ?>

In addition to the if statement, we add an else. This says that if $thumb is not empty, ie it has a defined ‘value’, then output that ‘value’ in the image source, and make it a link to the post.

This saves me a lot of time when posting, as I don’t need to find the url of my default image and add it to the custom fields, I can just post straight away, knowing that an image is going to be there where I need it.

31 Days to Build a Better Blog Affiliate Link

Did you enjoy this post? If you did, so might others! Please share it!

15 Comments - Add Yours!

  • Karl Bedford wrote on June 25 2008 at 7:55 pm | Permalink

    I was really scratching my head about this as I had posted asking how to do this exact thing on the WP forums but no one answered.

    Thank you so much for this. Saved me hours :)

    I used it in conjunction with TimThumb & Random File so I get resized random images for non-custom field images.

    Regards
    Karl

    • cherie wrote on October 12 2009 at 1:28 am | Permalink

      Hi, i’m completely new to .php and wondering where would i post this code at? post.php ?

      I’m wanting to just basically have my ‘thumb’ custom field to have a default of wp-content/random.jpeg

      how would i go about doing that? any help would be great =)

      • Alex wrote on October 13 2009 at 5:32 pm | Permalink

        Hi Cherie, you can put this anywhere you want to display the image, it really doesn’t matter. If you want the image on your homepage, then look at index.php and find the loop, or home.php if your theme has one.

        If you follow the instructions under default image display, replacing “http://domain.com/images/defaultimage.jpg” with your image, you should be well on your way!

        Let me know if you need any more specific instructions.

  • Per Grankvist wrote on August 17 2008 at 9:43 pm | Permalink

    Actually, the code should use single quotes, not double quotes, in the second else in the basic code;

    echo ‘whatever’ . $var . ‘whatever’;

    I didn’t and my page wouldn’t display at all.

  • zets wrote on August 27 2008 at 4:07 pm | Permalink

    Thanks a lot. But it wont work for me. Could you please reply with full code (all three parts together). Thanks a lot.

  • Fred wrote on November 29 2008 at 2:40 am | Permalink

    Hi! I ve been searching for this a long time, and you seem the only one that knows how to do it.
    The problem is that I am completely dumb, so, if you please, I need some help.
    I am using the theme mimbo and it has the thumbnail function where I use the custom field key=Image and value=image.jpg to display a thumb for the post. But I have a default image for each category, and it is really boring to enter the same value for each post, and the worst is that I cant do it when posting via email. So if there is anyway I can link a costum field for each category, it will be all happines.
    Thanks a lot !

  • Kai wrote on May 5 2009 at 12:28 am | Permalink

    Hello, I’ve manage to add the thumb next to the excerpt for the post. I was wondering if anyone know how I can add a line of text above the thumb, which can be a tag and linked to the relevant tag page (like how http://www.lifehacker.com has above each of the post’s thumb). Thanks!

  • Donna B. wrote on June 10 2009 at 2:17 am | Permalink

    Hello,
    thanks very much for putting this together, and linking to it on WordPress! I didn’t see this anywhere else, and metadata is really fun and important to tweak your blog the way you want to.

    I added weather (add on to the date) to my metadata with your help.

    • Alex wrote on July 2 2009 at 5:00 pm | Permalink

      @Donna – I’m glad you found it useful!

  • g wrote on July 2 2009 at 6:19 am | Permalink

    tried my darnest to add this to the magazeen theme for the header image dock. i just can’t seem to figure it out. if anyone does, i would be ever so grateful!

    http://wefunction.com/2009/02/free-theme-magazeen/

    • Alex wrote on July 2 2009 at 5:03 pm | Permalink

      @g – Sorry you’re having problems implementing it. Unfortunately I don’t really have time to dig into the theme right now. Does the theme have a support forum? If this is really pressing, please get in touch via my contact page, and I’ll see what I can do.

  • Tech News wrote on July 4 2009 at 9:17 pm | Permalink

    I found your blog on google and read a few of your other posts. I just added you to my Google News Reader. Keep up the good work. Look forward to reading more articles from you in the future.

    - Jack

  • Justin C wrote on July 27 2009 at 4:24 pm | Permalink

    I don’t understand what the actual code is and where to place it. Can you respond with the actual code I need to implement? Thanks!

    • Alex wrote on July 28 2009 at 11:23 pm | Permalink

      @Justin C – In theory, the code can be placed anywhere, be it in a header, page or sidebar – wherever it is that you are using the custom field. It’s a little bit difficult to post the ‘actual’ code because it depends on what you want to use it for. But if you use the ‘Basic Code’ as a base, then you can go from there. If you have something in mind that you’re struggling with, then comment back and I’ll try and help.

  • marujo wrote on November 20 2009 at 6:58 pm | Permalink

    hi. great article. im trying to use but i have a problem. im using these code in a page template (that have 2 customfields):

    ID, ‘imagem-titulo’, true); ?>
    <img src="ID, ‘imagem-titulo’, true); ?>” />

    ID, ‘flash-wide’, true); ?>
    ID, ‘flash-wide’, true); ?>

    but in some cases i dont need to use these customfileds. In FF its ok. But in IE appears a blank image error (red x) for each customfiled if is blank. How can i fix this, if the custom field is blank, the browser dont print none in the screen.

    thanks a lot!!

Post a Comment

Your email is never published nor shared.

*
*
User Gravatar