get custom fields easier hack in WordPress
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Open up your theme’s functions.php file and paste the following code somewhere between PHP tags:

function get_custom_field($key, $echo = FALSE) {
	global $post;
	$custom_field = get_post_meta($post->ID, $key, true);
	if ($echo == FALSE) return $custom_field;
	echo $custom_field;
}

Now, when you want to get the value of the custom field, you simply use the function in your theme files like so:

<?php get_custom_field('image', TRUE); ?>

Using the “TRUE” value makes sure that the function actually echos the value, rather than just returning it.

Source

You may want to check :

  1. display custom field
  2. Creating Custom Post Types
  3. Going Even Further With Custom Taxonomies AND Post Types
  4. get custom fields value
  5. display content only if a custom field exists

Post a Comment

Your email is never shared. Required fields are marked *

*
*