In WordPress, Custom Fields are known as meta-data which are some extra information for a post. It’s powerful feature in WordPress so everyone should know if you decided to use WordPress for your business.
Today, I will show how to add a custom field to a post and display it somewhere in your post detail page. And if you would like to get all custom fields for a particular post, we already have a solution for that.
1. Adding a Custom Field
1. When add/edit a post, scroll down to the area titled Custom Fields. If for some reason, your Custom Fields panel is hidden, read this post: http://4rapiddev.com/problem-issue-error/excerpt-box-disappeared-on-wordpress-new-version/ will help you how to enable it again.
2. To create a custom field named “featured_image” has value “http://4rapiddev.com/wp-content/uploads/2011/03/Facebook-Like-Button-On-Wordpress.jpg”, simply enter the text “featured_image” in the text field titled Name and enter the text “http://4rapiddev.com/wp-content/uploads/2011/03/Facebook-Like-Button-On-Wordpress.jpg” in the text field titled Value
3. Click Add Custom Field button to save the extra information for the post.
2. Displaying Custom Field
After create the custom field, it’s time to display it on your post. To display it on your post detail page, you need to know the ID of your current page with:
$post_id = $post->ID; |
Then use get_post_meta() function to get the value of a particular custom field with a specified key. In this case, you need to get the value of custom field named featured_image, so use this script:
$featured_image = get_post_meta($post_id, "featured_image", true); |
For more detail about the get_post_meta() function, go to this page.