Created
March 5, 2012 04:00
-
-
Save BronsonQuick/1976518 to your computer and use it in GitHub Desktop.
A widget to display a Featured Custom Post Type in WordPress
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: Featured Custom Post Type Widget | |
Plugin URI: http://www.sennza.com.au | |
Description: Lists out all the custom post type posts in a dropdown so the user can selected a CPT to feature. | |
Version: 1.0 | |
Author: Bronson Quick | |
Author URI: http://www.sennza.com.au | |
License: GPL2 | |
Use this with your custom post type and alter the 2 references to: 'post_type' => 'case_study' to your own CPT | |
The other 'gotcha' for this widget is that your Custom Post Type needs to have 'hierarchical' => 'true' in your | |
register_post_type function | |
*/ | |
class Sennza_Featured_Case_Study extends WP_Widget { | |
/** | |
* Register widget with WordPress. | |
*/ | |
public function __construct() { | |
parent::__construct( | |
'featured_case_study', // Base ID | |
'Featured Case Study', // Name | |
array( 'description' => __( 'A widget to display a featured case study', '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 ); | |
$featured_post = apply_filters( 'featured_post', $instance['featured_post'] ); | |
$args = array( | |
'p' => $featured_post, | |
'post_type' => 'case_study'); | |
$featured_case_study = new WP_Query( $args); | |
echo $before_widget; | |
if ($featured_case_study->have_posts()) : while ($featured_case_study->have_posts()) : $featured_case_study->the_post();?> | |
<h3><?php the_title();?></h3> | |
<?php the_excerpt(); ?> | |
<?php endwhile; endif; ?> | |
<?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 = $old_instance; | |
$instance['featured_post'] = strip_tags( $new_instance['featured_post'] ); | |
return $instance; | |
} | |
/** | |
* Back-end widget form. | |
* | |
* @see WP_Widget::form() | |
* | |
* @param array $instance Previously saved values from database. | |
*/ | |
public function form( $instance ) { | |
$featured_post = $instance['featured_post']; | |
$case_study_args = array( | |
'post_type' => 'case_study', | |
'posts_per_page' => '-1' | |
); | |
$case_studies = new WP_Query( $case_study_args ); | |
?> | |
<p> | |
<fieldset> | |
<label for="<?php echo $this->get_field_id( 'featured_post' ); ?>">Select a case study:</label> | |
<select id="<?php echo $this->get_field_id( 'featured_post' ); ?>" name="<?php echo $this->get_field_name('featured_post');?> "> | |
<?php if ($case_studies->have_posts()) : while ($case_studies->have_posts()) : $case_studies->the_post();?> | |
<option value="<?php the_ID(); ?>" <?php selected( $featured_post, get_the_ID()); ?>><?php the_title();?></option> | |
<?php endwhile; endif; ?> | |
</select> | |
</fieldset> | |
</p> | |
<?php | |
} | |
} | |
add_action( 'widgets_init', create_function( '', 'register_widget( "Sennza_Featured_Case_Study" );' ) ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment