WordPress Custom Keys by eckert
Custom keys are really easy and really handy.
If you want a placeholder on a page in WordPress, but want to be able to define what goes into it depending on the post, then just use a custom key.
Simply input the following code, replacing the XXX with something that will make sense later.
<?php $key="XXX"; echo get_post_meta($post->ID, $key, true); ?>
So if you are using this for a map, then I would use the key “map”.
You can place this on any php page, but I would suggest placing it within a <div>so that you can place it and style it later.
Next, scroll down on the write post page to where it says “Custom Fields”.
Write whatever your key is (map, from the example above) and then place the value after it.
One trick that I like to use is putting 90% of your code within the php page and just what you need in the value box.
An example of this would be if I wanted to use a custom key to place an image on the page. To do so, all you would need to do is this
<img src="http://yourdomain.com/wp-content/uploads/<?php $key="picture"; echo get_post_meta($post->ID, $key, true); ?>.jpg">
All you would have to do is then go to your custom fields, type in “picture” for the key and the title of the jpg in the value. That’s it.
If you have any questions, just comment below and I’ll be glad to help.
June 15th, 2009 at 10:26 am
Hi eckert
Thanks for the clear explanation. A question that will reveal my ignorance – sorry!–
Isn’t just as easy to simply put the image URL in the post ? Why go to the hassle of setting up a custom key to accomplish this?
Second question: (again -sorry about the level of ignorance): Regarding custom keys and use of wpdb objects to access them – lets say there are several custom keyed list items to list on the sidebar just as an example – should you do repetitive queries to get each, or one big query with several joins and then loop the array of results to get them? Probably not making myself clear. If you do repetitive queries do you have to flush or reset the mySQL database search between each query? And I can’t find any info about the structure of the query $object result ($pageposts for example) – to know how to get at the array.
Finally there seems to be a delay between adding a key/value to a page and the appearance of the desired list item on the site. Why?
Perhaps there’s a tutorial on this other than that which appears on the Codex but I haven’t been able to find such
thanks very much in advance
Bob
July 7th, 2009 at 4:02 am
Interesting Article. Thanks for the read!
January 13th, 2010 at 10:56 pm
i confuse about the code that u suggest to put – “ID, $key, true); ?>”
and
<img src=”http://yourdomain.com/wp-content/uploads/ID, $key, true); ?>.jpg”>
where should i put this code?? and wat value i put at the “custom field”..my theme request me to put custom key to make the image preview posible but i have no idea wat is custome key is..
January 14th, 2010 at 1:02 pm
you can put the code anywhere in a php page that you would like your custom key to appear. the first is an example in which you would put anything into the custom key.
the second example shows you how you how you could use the could to place an image on your page. the code would be placed into whichever .php file it correlates to and the custom key would be placed into a post.
March 4th, 2010 at 2:24 pm
Great information was looking for this Thanks again and keep up the good work!