proweb
8/11/2011 - 7:12 PM

This is a wordpress function that will return the parent of the current page.

This is a wordpress function that will return the parent of the current page.

<?php

/**
 * Function is responsible for returning the parent of the current
 * page.
 * 
 * This function should be placed in your functions.php file, or equivalent
 * 
 * @param unknown_type $post
 * @return object
 */
function get_post_parent( $post=null )
{
	//initializing
	global $wp_the_query;
	if (!$post)
		$post = $wp_the_query->post;
	
	//reasons to return false
	if (!is_object($post) || is_wp_error($post)) return false;
	if (!$post->post_parent) return false;
	
	//load and return the parent
	$parent = get_post($post->post_parent);
	return $parent;
}

/**
 * Function is responsible for returning the parent page if it exists or
 * returning the current page if the parent does not exist.
 * 
 * This function should be placed in your functions.php file, or equivalent
 * 
 * @return object
 */
function get_highlighted_excerpt()
{
	if (!$post = get_post_parent())
	{
		global $post;
	}
	return $post;
}

// these next lines of code should be placed in the location that you 
// want to output the parent title and excerpt
// if the parent page exists
if ($parent = get_highlighted_excerpt())
{
	echo "<h1>{$parent->post_title}</h1>",
		"<p>{$parent->post_excerpt}</p>";
}