get wordpress custom fields outside the loop

June 12, 2009 · Print This Article

Custom fields are very useful and are used on many WordPress blogs.

But how to easily get custom fields values outside the loop?

To display a custom field value outside the loop, simply use the following code.

Don’t forget to replace Myfield on line 4 by the name of the custom field you want to display.

 

<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'MyField', true);
?>


Your choice for site templates and wordpress themes

Comments

One Response to “get wordpress custom fields outside the loop”

  1. The Power of Custom Fields - Idealien Studios on July 21st, 2009 9:30 am

    […] LiveXP shows how to get custom fields outside the loop. If creating a theme options page is beyond your skill, you can create a private page and store / retrieve the meta values from custom fields attached to that page wherever you need to. […]

Got something to say?

You must be logged in to post a comment.