Last active
August 29, 2015 14:05
-
-
Save birgire/dd1b31a5f7525ed4bbf6 to your computer and use it in GitHub Desktop.
WordPress: Child Pages Meta Box For Hierarchial Post Types
This file contains hidden or 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 | |
/** | |
* Plugin Name: Child Pages Meta Box | |
* Description: Child pages meta box for hierarchial post types | |
* Author: Birgir Erlendsson (birgire) | |
* Plugin URI: http://wordpress.stackexchange.com/a/158636/26350 | |
* Version: 0.0.2 | |
*/ | |
function wpse_current_child_pages_meta_box() | |
{ | |
$post_types = get_post_types(); | |
foreach ( $post_types as $post_type ) | |
{ | |
if( is_post_type_hierarchical( $post_type ) ) | |
{ | |
add_meta_box( | |
'wpse_child_pages', | |
__( 'Current child pages' ), | |
'wpse_list_current_child_pages', | |
$post_type, | |
'side', | |
'low' | |
); | |
} | |
} | |
} | |
add_action( 'add_meta_boxes', 'wpse_current_child_pages_meta_box' ); | |
function wpse_list_current_child_pages( $post ) | |
{ | |
$args = array( | |
'child_of' => $post->ID, | |
'echo' => 0, | |
'title_li' => '', | |
'post_type' => $post->post_type, | |
'walker' => new WPSE_EditLinks | |
); | |
$style = '<style>.wpse_childpages li {margin-left: 15px;}</style>'; | |
$list = wp_list_pages( $args ); | |
if( ! $list ) | |
$list = sprintf( '<li>%s</li>', __( 'No child pages found!' ) ); | |
printf( '%s<ul class="wpse_childpages">%s</li>', $style, $list ); | |
} | |
class WPSE_EditLinks extends Walker_Page | |
{ | |
function end_el( &$output, $page, $depth = 0, $args = array() ) | |
{ | |
$edit_url = admin_url( 'post.php?action=edit&post=' . $page->ID ); | |
$edit_link = " <a class='dashicons dashicons-edit' title='edit' href='" . esc_url( $edit_url ) . "' target='_blank'></a> "; | |
$output = str_replace( "><a", ">". $edit_link. "<a", $output ); | |
$output .= "</li>\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment