Skip to content

Instantly share code, notes, and snippets.

@GaryJones
Last active December 16, 2015 00:49
Show Gist options
  • Select an option

  • Save GaryJones/5350636 to your computer and use it in GitHub Desktop.

Select an option

Save GaryJones/5350636 to your computer and use it in GitHub Desktop.
<?php
add_action( 'genesis_before_post', 'dc_blah_feat' );
/**
* Echo parent page post thumbnail for a page.
*/
function dc_blah_feat () {
if ( is_page() )
echo prefix_get_page_parent_featured_image();
}
/**
* Get featured image of current page's parent page.
*
* @return string Post thumbnail markup, or empty string if not currently on a page.
*/
function prefix_get_page_parent_featured_image() {
if ( ! is_page() )
return '';
$parents = get_post_ancestors( get_the_ID() );
$id = $parents ? $parents[ count( $parents ) - 1 ] : get_the_ID();
return get_the_post_thumbnail( $id, 'full' );
}
@GaryJones
Copy link
Copy Markdown
Author

This is a couple of extra lines of code longer than the original, but you get:

  • Re-usable function that can find the parent page featured image for all themes, not just Genesis, and one that can be hooked in to multiple places, not just before post in Genesis.
  • Code standards.
  • Documentation.
  • Avoidance of global $post.

@cdils
Copy link
Copy Markdown

cdils commented Apr 13, 2013

Thanks again, Gary. Just used this in a slightly different way. Does the conditional check prevent the need for the global $post?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment