Skip to content

Instantly share code, notes, and snippets.

@torounit
Last active December 17, 2015 01:49
Show Gist options
  • Save torounit/5531273 to your computer and use it in GitHub Desktop.
Save torounit/5531273 to your computer and use it in GitHub Desktop.
WordPressの階層のある投稿タイプで、最上位の親投稿のIDを返す。階層の無い投稿タイプの場合 falseを返す。
<?php
function current_hierarchical() {
global $post;
$post_type_object = get_post_type_object( get_post_type() );
if ( $post_type_object->hierarchical ) {
if ( $post->post_parent ) { // is child page
$ancestors = get_post_ancestors( $post->ID );
$root_ID = array_pop( $ancestors );
}
else { // is root page
$root_ID = $post->ID;
}
return $root_ID;
}
return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment