Skip to content

Instantly share code, notes, and snippets.

@rob-gordon
Last active March 30, 2016 12:55
Show Gist options
  • Select an option

  • Save rob-gordon/0b59bde9957080ec5fc49e86afe04189 to your computer and use it in GitHub Desktop.

Select an option

Save rob-gordon/0b59bde9957080ec5fc49e86afe04189 to your computer and use it in GitHub Desktop.
function climb_and_return($elem, $test_fn, $default) {
$test = false;
while(!$test && $elem !== 0) {
$test = call_user_func($test_fn, $elem);
if ($test == false) {
if (is_page()) {
$elem = $elem->post_parent == 0 ? 0 : get_post($elem->post_parent);
} else if (is_single()) {
if ($elem->post_parent) {
$elem = get_post($elem->post_parent);
} else {
// theoretically we shoudl climb to the taxonomy level now
$elem = 0;
}
} else {
$elem = 0;
$test = false;
}
}
}
if ($test != false) {
return $test;
} else {
return $default;
}
}
/*----------*/
function my_has_featured_image($elem) {
if (has_post_thumbnail( $elem->ID )) {
return get_post_thumbnail_id( $elem->ID );
} else {
return false;
}
}
/*-----------*/
$thumbnail_id = climb_and_return($post, 'my_has_featured_image', 142);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment