Created
August 11, 2011 19:12
-
-
Save jonathonbyrdziak/1140481 to your computer and use it in GitHub Desktop.
This is a wordpress function that will return the parent of the current page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Let me know if this helped you at all. You can always tip me via paypal at jonathonbyrd@gmail.com