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.