Free Wordpress Blog Setup

How to Display Custom Fields Outside The Loop in WordPress

By Editorial Staff in Themes
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.

What Next?

Digg it
Save This Page
Subscribe to WPBeginner
Stumble it
Free Wordpress Blog Setup

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.

Due to high volume of request from our readers, we are adding this feature that allows you to stay updated with this post's comments without having to participate in the discussion even though we would love your input as always. Don't worry we hate SPAM just as much as you do, so you will never receive any SPAM messages from our site and that's our promise to you.

Subscribe without commenting

Close Bar