I have the following widget, the way it works is that you have to enter the page ID in order to pull that pages info. I'd like it to instead have a drop down selection of all the pages and be able to select the page that way:
<?php
// register widget
add_action( 'widgets_init', 'feat_page_load_widgets' );
function feat_page_load_widgets() {
register_widget( 'Widget_Custom_Featured_Page' );
}
// widget class
class Widget_Custom_Featured_Page extends WP_Widget {
function Widget_Custom_Featured_Page() {
// widget settings
$widget_ops = array('classname' => 'Widget_Custom_Featured_Page', 'description' => __( 'A featured page with thumbnail') );
// create the widget
$this->WP_Widget('custom_featured_page', __('Featured Page'), $widget_ops);
}
/**
* How to display the widget on the screen.
*/
function widget( $args, $instance ) {
extract( $args );
// vars from widget settings
$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Featured Page' ) : $instance['title'], $instance, $this->id_base);
$page_title = empty( $instance['page_title'] ) ? '' : $instance['page_title'];
$post_id = empty( $instance['post_id'] ) ? '' : $instance['post_id'];
// query
$feat_query = array(
'posts_per_page' => 1,
'nopaging' => true,
'post_type' => 'page',
'page_id' => $post_id
);
// fire query
$feat_posts = new WP_Query();
$feat_posts->query($feat_query);
// output
if ($feat_posts->have_posts()) {
echo $before_widget;
/* widget title
if ( $title)
echo $before_title . $title . $after_title; */
// start loop
while ($feat_posts->have_posts()) : $feat_posts->the_post();
// wrapping div
echo '<div class="featured_page">';
// thumbnail
if(has_post_thumbnail())
echo '<a href="' . get_permalink() . '">' .get_the_post_thumbnail($post->ID, 'small_photo'). '</a>';
// page title
if ( $page_title) {
echo '<h3><a href="' . get_permalink() . '">' .$page_title. '</a></h3>';
} else {
echo '<h3><a href="' . get_permalink() . '">' .get_the_title(). '</a></h3>';
}
// end wrapping div
echo '</div>';
// end loop
endwhile;
echo $after_widget;
}
}
/**
* Update the widget settings.
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['page_title'] = strip_tags($new_instance['page_title']);
$instance['post_id'] = strip_tags( $new_instance['post_id'] );
return $instance;
}
/**
* Displays the widget settings controls on the widget panel.
* Make use of the get_field_id() and get_field_name() function
* when creating your form elements. This handles the confusing stuff.
*/
function form( $instance ) {
//Defaults
$title = esc_attr( $instance['title'] );
$page_title = esc_attr( $instance['page_title'] );
$post_id = esc_attr( $instance['post_id'] );
?>
<?php /* <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p> */ ?>
<p>
<label for="<?php echo $this->get_field_id('post_id'); ?>"><?php _e( 'Page ID:' ); ?></label> <input type="text" value="<?php echo $post_id; ?>" name="<?php echo $this->get_field_name('post_id'); ?>" id="<?php echo $this->get_field_id('post_id'); ?>" class="widefat" />
</p>
<p><label for="<?php echo $this->get_field_id('page_title'); ?>"><?php _e('Alternate Page Title (not required):'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('page_title'); ?>" name="<?php echo $this->get_field_name('page_title'); ?>" type="text" value="<?php echo $page_title; ?>" /></p>
<?php
}
} ?>
Khanh Cao answers:
In Form function, change:
<input type="text" value="<?php echo $post_id; ?>" name="<?php echo $this->get_field_name('post_id'); ?>" id="<?php echo $this->get_field_id('post_id'); ?>" class="widefat" />
to:
<?php
$drop_name = $this->get_field_name('post_id');
wp_dropdown_pages('name=' . $drop_name);
?>
Jake Caputo comments:
Awesome. This almost works.
The only problem is that when you save it, the drop down reverts back to the default item; although on the front end it still displays the page that was saved.
Khanh Cao comments:
My bad,
It should be
<?php
$args = array(
'name' => $this->get_field_name('post_id'),
'selected' => $post_id
);
wp_dropdown_pages($args);
?>
Jake Caputo comments:
That did it! Thanks!