Last active
August 29, 2015 14:27
-
-
Save rbk/f11ada0ec92274ae3164 to your computer and use it in GitHub Desktop.
Widget with option to select a parent page. The widget shows the parent with a list of subpages below.
This file contains 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: Subpage List Widget | |
Description: A widget to list the subpages of a page. | |
Version: 1 | |
Author: GuRuStu.co | |
Author URI: http://gurustu.co | |
*/ | |
/** | |
* Adds Featured_Pages_Widget widget. | |
*/ | |
class GRS_Subpage_List_Widget extends WP_Widget { | |
/** | |
* Register widget with WordPress. | |
*/ | |
public function __construct() { | |
parent::__construct( | |
'grs_subpage_list_widget', // Base ID | |
'Subpage List Widget', // Name | |
array( 'description' => __( 'A widget to list the subpages of a page. ', 'text_domain' ), ) // Args | |
); | |
} | |
/** | |
* Front-end display of widget. | |
* | |
* @see WP_Widget::widget() | |
* | |
* @param array $args Widget arguments. | |
* @param array $instance Saved values from database. | |
*/ | |
public function widget( $args, $instance ) { | |
extract( $args ); | |
echo $before_widget; | |
// This is the output for the widget on the frontend | |
?> | |
<div class="col-md-4"> | |
<h1 class="footer-heading"> | |
<a class="footer-heading" href="<?php echo get_permalink($instance['page_id']); ?>"><?php echo get_the_title($instance['page_id']); ?></a> | |
</h1> | |
<ul> | |
<?php wp_list_pages("title_li=&child_of=" . $instance['page_id']); ?> | |
</ul> | |
<br> | |
</div> | |
<?php | |
echo $after_widget; | |
} | |
/** | |
* Sanitize widget form values as they are saved. | |
* | |
* @see WP_Widget::update() | |
* | |
* @param array $new_instance Values just sent to be saved. | |
* @param array $old_instance Previously saved values from database. | |
* | |
* @return array Updated safe values to be saved. | |
*/ | |
public function update( $new_instance, $old_instance ) { | |
$instance = array(); | |
$instance['page_id'] = $new_instance['page_id']; | |
return $instance; | |
} | |
/** | |
* Back-end widget form. | |
* | |
* @see WP_Widget::form() | |
* | |
* @param array $instance Previously saved values from database. | |
*/ | |
public function form ( $instance ) { ?> | |
<p> | |
<label>Select a parent page:</label> | |
<select name="<?php echo $this->get_field_name('page_id'); ?>" id="<?php echo $this->get_field_id('page_id'); ?>"> | |
<?php | |
$id = $instance['page_id']; | |
$pages = get_posts(array('post_type'=>'page', 'posts_per_page'=> -1)); | |
foreach( $pages as $page ){ | |
$children = get_posts(array('post_type'=>'page', 'post_parent' => $page->ID)); | |
if( count($children) > 0 ){ | |
if( $page->ID == $id ){ | |
$selected = 'selected'; | |
} else { | |
$selected = ''; | |
} | |
echo '<option value="'.$page->ID.'" ' . $selected . '>'.$page->post_title.'</option>'; | |
} | |
} | |
?> | |
</select> | |
</p> | |
<?php | |
} | |
} // class | |
add_action( 'widgets_init', function(){ | |
register_widget('grs_subpage_list_widget'); | |
}); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment