0815

Du musst dich anmelden um Beiträge und Themen zu erstellen. Einloggen · Registrieren

Access Post Data Outside the Loop

The easiest way to access and display post data outside of 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 this function in your functions.php and then add this 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).




0