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.
Comments
No Responses to “How to Display Custom Fields Outside The Loop in WordPress”Share Your Opinions
Tell us what you're thinking...
and if you want a pic to show with your comment, then get gravatar!
Please make sure that you have read our Comment Policy.










What Next?
Digg itSave This Page
Subscribe to WPBeginner
Stumble it
Related Posts