Think of yourself as a Blogger on the move. You are traveling around the world, sometimes you are at Berlin, and then you move to Paris, yet again to New York.
How cool it would be to add a note to each post that says where you wrote that from? Or maybe you might want to show off your mood, in the mySpace style or insert answers, “What’s on your mind?” or “What are you doing?” with every post.
This can be done using Custom Fields.
We will use
get_post_meta($post_id, $key, $single); for our purpose. It’s a function that’s used to fetch the post meta data.
The code for our use will be:
get_post_meta($post->ID, "note", true);
The post id is the id of the post. It can be any number and the data from the meta of the corresponding post will be used.
In our case, it’s the post that calls the function, use $post->ID. ‘note’ would act as the Custom Field KEY. You can change it to anything you like, and your custom field KEY would be that one.
Insert the following code into any of your post page of your theme file, either single.php or index.php (for StudioPress themes).
< ?php if( get_post_meta($post->ID, "note", true) ): ? >
Posted From: < ?php echo get_post_meta($post->ID, "note", true); ? >
< ?php endif; ? >
The ‘if’ conditions would check if the custom field exists, if it doesn’t exist, nothing will be displayed.
Have fun! 🙂