How To Display Post Data Outside The Loop

Add Tool

Display Post Data Outside The Loop WordPress


Display Post Data Outside The Loop WordPress

The easiest way to display and access  post data outside the loop is to use the built-in WordPress core function get_post(); however, for more control over the process, check out this nifty little function from WP Recipes:

function get_post_data($postId) {
	global $wpdb;
	return $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID=$postId");
}


Place the above function in your functions.php and then add the below code to the desired location outside of the loop:

<?php $data = get_post_data(77);
	echo $data->post_date;     // post date
	echo $data->post_title;    // post title
	echo $data->post_content;  // post content
	echo $data->comment_count; // comments number
?>


For the argument of the function, specify the post ID for which you would like to display data. The function will return an array containing all of the available fields for the specified post (post_title, date, content, author_id, post_id, etc).


Tags: database, function, functions.php, Snippets, specified post, WordPress, wordpress post, WpRecipes
Payza

About Masum Billah

Display Post Data Outside The Loop WordPress 1stWebSuite website seo tool is a free website ranking tool site provides a lot of on page and off page seo useful tools for you. On this site, we have a large number of website ranking tools


SIGN UP TO GET UPDATE TO YOUR INBOX




Discuss about: How To Display Post Data Outside The Loop


adf.ly