How to Display Custom Fields Outside The Loop in WordPress

Posted on March 5th, 2010 by in Themes | 13 Comments  
How to Display Custom Fields Outside The Loop in WordPress

There will be times that you need to display post-specific content for example a different sidebar for a specific post. Usually post specific contents are added via a custom field which can only be displayed inside a post loop. In this article we will show you how you can display custom fields outside the loop. This tutorial will allow you to expand the possibilities of what you can do with WordPress.

The code will look like this:

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

You will need to make sure that you change the part where it says “Your-Custom-Field”.

This code is just the basic code. You can play around with it and use all the query_post arguments to fit your needs. You can even display another post’s custom fields on a single page of a different post.

In this (Example) at Uzzz, we are displaying the custom fields of a specific portfolio post, into the single post of a related testimonial post.

This snippet is very useful for theme developers. If you have any questions, feel free to ask in the comment or via email.

About

Editorial Staff at WPBeginner mainly Syed and David.

Post comment as twitter logo facebook logo
Sort: Newest | Oldest
efgamercity 5 pts

Cheers for the code! I needed this for a game-info plugin I was working on and it worked a treat! :D

This has worked for me and it's awesome! I just had a question though, because this seems to work on every page except my blog page, (index.php). I think it's because I'm using a different loop on index.php Anyone else had this issue?

Rick

Hi,

Where do you put the code? - which php section are you editing?

Thank you

You add the code where you want to display it. It can vary on each use.

What about displaying custom fields outside the Loop, with conditional statements? I saw your post on conditional statements as well, but I don't understand the context well-enough to execute.

Very useful! Can you help get to this last point?

Thanks in advance!

Rick

Works a treat - thanks! I'm now doing all kinds of crazy things with custom fields in my sidebar :)

How do I link an mp3 hyperlink (on a page) from a ‘mp3′ custom field I have in a post?

I’m not sure how close I am, but here’s what I would like to do:

<a href="ID;
echo get_post_meta(2437, ‘mp3′, true);
?>”>

the audio player automatically shows up if it sees a hyperlink (I’ve been using absolute http:// values), but all I see with this is code on my webpage.

I’ve also tried it with HTML 5 audio tag:

ID;
echo get_post_meta(2437, ‘mp3′, true);
?>” controls=”controls”>

Any help is appreciated!

You are not suppose to put the post ID. Let it be dynamic with the $postid variable.

i don't understand what that means. i've tried everything. can you write out the example please?

Tweets about us: