Skip to content

Instantly share code, notes, and snippets.

@cadic
Created November 10, 2015 11:35
Show Gist options
  • Save cadic/043cd88bbfd48ec9c958 to your computer and use it in GitHub Desktop.
Save cadic/043cd88bbfd48ec9c958 to your computer and use it in GitHub Desktop.
Child pages list widget
<?php
/*
Plugin Name: Siblings Navi Widget
Description: Widget with links to all childern of top-level parent or self page
Author: Max Lyuchin
Author URI: http://heartwp.com/
*/
class ML_SiblingsNaviWidget extends WP_Widget {
function __construct() {
// Instantiate the parent object
parent::__construct( false, 'Siblings Navi Widget' );
}
function widget( $args, $instance ) {
global $id;
// Fire early if home or not a page
if ( is_home() || !is_page() || !$id )
return;
$ancestors = get_post_ancestors( $id, 'page' );
if ( $ancestors ) {
$parent_id = end( $ancestors );
} else {
$parent_id = $id;
}
$widget_title = get_the_title( $parent_id );
$widget_items = wp_list_pages( array(
'echo' => 0,
'child_of' => $parent_id,
'title_li' => '',
) );
if ( $widget_items ) {
// Output
echo $args['before_widget'];
echo $args['before_title'];
echo $widget_title;
echo $args['after_title'];
echo $widget_items;
echo $args['after_widget'];
}
}
}
function ml_siblings_register_widgets() {
register_widget( 'ML_SiblingsNaviWidget' );
}
add_action( 'widgets_init', 'ml_siblings_register_widgets' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment