Skip to content

Instantly share code, notes, and snippets.

@Jany-M
Created April 8, 2016 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Jany-M/32e327d6c6a0eac149a83360fc8bb010 to your computer and use it in GitHub Desktop.
Save Jany-M/32e327d6c6a0eac149a83360fc8bb010 to your computer and use it in GitHub Desktop.
[WordPress] Display Post or Page Children in custom MetaBox
<?php
// This requires that your post type is hierarchical obviously (like Pages)
// Display all children of this post/page in a custom metabox below title
function display_msg_for_parents( $post ) {
$children = get_pages(array('child_of' => $post->ID, 'post_type' => get_post_type($post->ID),));
if(!empty($children)) { ?>
<div class="rp_children postbox">
<button class="handlediv button-link" aria-expanded="true" type="button">
<span class="screen-reader-text">Toggle panel: Children</span>
<span class="toggle-indicator" aria-hidden="true"></span>
</button>
<h2 class="hndle ui-sortable-handle">
<span>Children</span>
</h2>
<div class="inside">
<p>This Post has the following children:</p>
<ul>
<?php foreach($children as $child) {
echo '<li><a href="'.get_edit_post_link($child->ID).'" target="_blank">'.$child->post_title.'</a></li>';
} ?>
</ul>
</div>
</div>
<?php }
wp_reset_postdata();
}
add_action( 'edit_form_after_title', 'display_msg_for_parents' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment