Skip to content

Instantly share code, notes, and snippets.

@josephhinson
Created December 6, 2013 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephhinson/7827755 to your computer and use it in GitHub Desktop.
Save josephhinson/7827755 to your computer and use it in GitHub Desktop.
Widget that allows users to pull featured image and link for either a selected page, or the latest blog post. I wrote this for a custom homepage widget, but it might be useful for something else later -- especially the per-page functionality.
<?php
// This is a widget for Homepage Widgets
// initializes the widget on WordPress Load
add_action('widgets_init', 'homepage_widget_init_widget');
// Should be called above from "add_action"
function homepage_widget_init_widget() {
register_widget( 'HomepageWidget' );
}
// new class to extend WP_Widget function
class HomepageWidget extends WP_Widget {
/** Widget setup. */
function HomepageWidget() {
/* Widget settings. */
$widget_ops = array(
'classname' => 'homepage_widget',
'description' => __('Homepage Widgets', 'homepage_widget') );
/* Widget control settings. */
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'homepage_widget' );
/* Create the widget. */
$this->WP_Widget( 'homepage_widget', __('Homepage Widget', 'Options'), $widget_ops, $control_ops );
}
/**
* How to display the widget on the screen. */
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', $instance['title'] );
$selected = $instance['content_type'];
$pages = $instance['pages'];
$summary = $instance['summary'];
echo $before_widget;
?>
<div class="widget-inner">
<?php
if ($selected == 'blog'):
$query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 1
));
else :
$query = new WP_Query(array(
'p' => $pages,
'post_type' => 'page',
'posts_per_page' => 1
));
endif;
if ($query->have_posts()) : while ($query->have_posts()) : $query-> the_post(); ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(array(105,105), array('title' => $post->post_title)); ?>
</a>
<?php
if ( $title )
echo $before_title . $title . $after_title; ?>
<p>
<?php if ($selected == 'page'): ?>
<?php echo $summary; ?>
<?php else : ?>
<?php the_title(); ?>
<?php endif; ?>
</p>
<p><a class="btn btn-white" href="<?php the_permalink(); ?>">Read More</a></p>
<?php endwhile; ?>
<?php endif; ?>
</div><!--end widget-inner -->
<?php
/* Before widget (defined by themes). */
/* Display name from widget settings if one was input. */
// Settings from the widget
/* After widget (defined by themes). */
echo $after_widget;
}
/**
* Update the widget settings.
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
/* Strip tags for title and name to remove HTML (important for text inputs). */
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['content_type'] = $new_instance['content_type'];
$instance['pages'] = $new_instance['pages'];
$instance['summary'] = $new_instance['summary'];
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 = array(
'title' => __('Homepage Widgets', 'homepage_widget'),
'content_type' => 'page'
);
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<!-- Widget Title: Text Input -->
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'homepage_widget'); ?></label><br>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'content_type' ); ?>">Select the type of content this widget will display</label>
<select name="<?php echo $this->get_field_name( 'content_type' ); ?>" id="<?php echo $this->get_field_id( 'content_type' ); ?>">
<option <?php if ($instance['content_type'] == 'blog') :?>selected <?php endif; ?> value="blog">First Blog Post</option>
<option <?php if ($instance['content_type'] == 'page') :?>selected <?php endif; ?> value="page">Selected Page</option>
</select>
</p>
<p class="pages_container">
<select style="width: 80%;" id="<?php echo $this->get_field_id( 'pages' ); ?>" name="<?php echo $this->get_field_name( 'pages' ); ?>" id="pages">
<?php $pages = get_posts('numberposts=-1&orderby=menu_order&order=ASC&post_type=page&post_status=publish');?>
<option value="0">Select the page you'd like to use</option>
<?php foreach ($pages as $page): ?>
<option <?php if ($instance['pages'] == $page->ID): ?>selected <?php endif ?>value="<?php echo $page->ID; ?>"><?php echo $page->post_title; ?></option>
<?php endforeach; ?>
</select><br><br>
<label for="<?php echo $this->get_field_id( 'summary' ); ?>"><?php _e('Summary Text:', 'homepage_widget'); ?></label><br>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'summary' ); ?>" name="<?php echo $this->get_field_name( 'summary' ); ?>" value="<?php echo $instance['summary']; ?>">
</p>
<?php
}
} // END HomepageWidget
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment