/ Published in: PHP
A simple widget for displaying a single post in a dynamic sidebar. Originally developed to accommodate a WP user who wanted full control of which posts were displayed and what order they were on a certain page.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /* Plugin Name: Single Post Plugin URI: http://ch4ze.com Description: Define a widget for displaying a single post Author: Chase Crawford Author URI: http://ch4ze.com Version: 0.1 */ class singlePostWidget extends WP_Widget { function singlePostWidget() { $this->WP_Widget('singlePostWidget', 'Single Post', $widget_ops); } function form($instance) { $pid = $instance['pid']; if($pid == '') { 'post_type' => 'post' ); $the_query = new WP_Query($args); print '<select name="' . $this->get_field_name('pid') . '"><option value ="">Choose a post to display</option>'; while ( $the_query->have_posts() ) : $the_query->the_post(); print '<option value="' . get_the_ID() . '">' . get_the_title() . '</option>'; endwhile; print '</select>'; } else { $post = query_posts('p=' . $pid); while ( have_posts() ) : the_post(); print "<strong>" . get_the_title() . "</strong>"; endwhile; } } function update($new_instance, $old_instance) { $instance = $old_instance; $instance['pid'] = $new_instance['pid']; return $instance; } function widget($args, $instance) { $post = query_posts('p=' . $instance['pid']); while ( have_posts() ) : the_post(); /*======================================================= add template code to display your post here =======================================================*/ endwhile; } }