Wordpress and the custom fields, a overlooked feature

2007-09-25 13:34 | Categories » English, Webb, Wordpress,

Wordpress logoI’ve been using Wordpress for about two years and I really like the possibilities with the system. This site is built from scratch, and my theme has gotten a few small upgrades over the years. The AJAX search, custom RSS feeds and so on. It is very common that Wordpress users get stuck in the “plugin hell”. They want a feature, but since they can’t find a suitable plugin for Wordpress they just ignore the need.

Sure, plugins sure make the customization of Wordpress easier, but many things can easily be fixed with one row of PHP. The thumbnails I have in the article listing are a solution of that kind.

I am talking if the custom fields in Wordpress, sadly often overlooked.

In my Wordpress install I have created a custom field for the thumbnail picture, let’s say it is called “thumbnail”. For all posts where I wish to use a custom thumbnail I just enter the path to the desired thumbnail image in my custom field. The path is obtained from the Wordpress edit-pane for the image; the part that I use is “/wp-content/uploads/2008/08/image.gif”. I know that it is possible to make the script to only need the filename, but that will require some extra code.

In the article listing (or wherever I wish to use it) I just use the following row of PHP:

echo get_post_meta($post->ID, "thumbnail", true);

That will print the value from that posts custom field named “thumbnail”. I combine that with some more code to get a valid img-tag:

<?php if(get_post_meta($post->ID, "thumbnail", true)) {
$size = getimagesize($_SERVER["DOCUMENT_ROOT"].get_post_meta($post->ID, “thumbnail”, true)); ?>
<img src=”<?php echo get_post_meta($post->ID, “thumbnail”, true); ?>” alt=”" width=”<?php echo $size[0]; ?>” height=”<?php echo $size[1]; ?>” />
<?php } ?>

So, what does the code do? The if-statement checks if the thumbnail custom field has a value at all. If so the size variable gets initiated with the array from the getimagesize function. Then the img tag is printed, filled with the path to the image and gets the width and height values from the size array we initiated earlier.

The custom fields really are a very powerful feature of Wordpress. Use your Imagination, think outside the box! You can use it for metadata related to the post, you can manage different language versions of the same post and so on.

If you have any questions or want to contribute in some way, use the comment form.

Digg it if you like it! =)

Don’t miss the other english articles.

Dela med dig
Gravatar
|
Add extra features to your Wordpress blog with custom fields « Blogs — 2007-10-31 01:22

[...] read more | digg story [...]

Gravatar
|
FeralPundit — 2007-11-24 20:23

Sounds like a good idea

Gravatar
|
Wolly’s Delicious » Blog Archive » links for 2007-11-26 — 2007-11-26 01:39

[...] Wordpress and the custom fields, a overlooked feature that (tags: wordpress thumbnail) [...]

Gravatar
|
Link notes del 4 2 2008 » Sapientone — 2008-02-05 01:31

[...] Wordpress and the custom fields, a overlooked feature - [...]

Gravatar
|
Ryan — 2008-02-06 02:11

Excellent. Thanks for the tips.

This worked perfectly for me :)

Gravatar
|
Kristoffer — 2008-02-06 13:24

Ryan: You’re welcome! :)
I’m glad it helped you!

Gravatar
|
Sean — 2008-02-08 00:12

Is there any way to associate thumbnails with the NEXT and PREVIOUS links in posts? I’m using:

<?php previous_post_link(’PREVIOUS EPISODE: %link’) ?>

<?php next_post_link(’NEXT EPISODE: %link’) ?>

but can’t figure out how to include the corresponding thumbnail with each link.

Gravatar
|
Charles — 2008-02-19 19:08

Do you know how to make the custom field.. say an image.. show up in the RSS feed? I am using custom fields for thumbnails but I don’t know how to make the custom thumbnails appear in the RSS feeds!

Thanks so much!

Charles

Gravatar
|
Kristoffer — 2008-02-19 21:04

Charles: I guess that the quickest way is to edit the php files that serves the feeds. They can be found in the folder wp-includes.

  • feed-atom.php
  • feed-rss.php
  • feed-rss2.php
  • feed-rdf.php

The part you should edit is the one where summary/description is printed out. If you need more help, don’t hesitate to use my contact form.

Gravatar
|
63 Essential Wordpress Hacks, Tutorials, Help Files and Cheats | Speckyboy - Wordpress and Design — 2008-03-09 08:13

[...] Wordpress and the custom fields, a overlooked feature (Source: Gate [...]

Gravatar
|
63 essenziali Wordpress Hacks, Tutorials, Help Files e Cheats : technorati.it — 2008-03-13 08:41

[...] Wordpress and the custom fields, a overlooked feature (Source: Gate [...]

Gravatar
|
Wordpress Tutorials/Hacks « The house of software freeware — 2008-03-13 09:52

[...] Wordpress and the custom fields, a overlooked feature (Source: Gate [...]

Gravatar
|
63 Essential Wordpress Hacks, Tutorials, Help Files and Cheats | Speckyboy - Wordpress and Design » 쟈스민(JASMIN) — 2008-03-23 15:43

[...] Wordpress and the custom fields, a overlooked feature (Source: Gate [...]

Gravatar
|
Tim — 2008-04-12 23:14

Great article thank you. I used your tips to add functionality in my plugin that thumbnails from images attached to a post (by passing the custom field) to now allow users to define an alternative image using a custom field in the way you describe. Thanks again

Leave a comment


Regler för kommentarer på Gate 303

Stäng
  • Sociala Bokmärkestjänster
  • E-mail
Maila
Creeper