I am building a child theme at http://habitat.erniehigh.com for a client. I need to add custom widget that shows recent posts by category. I have built similar widgets elsewhere using basically the same code.
class habitat_posts extends WP_Widget {
function habitat_posts(){
$widget_ops = array(
'description' => 'Allows you to display a list of recent posts within a particular category.');
$this->WP_Widget('fromcat-widget', 'Recent posts from a Category', $widget_ops);
}
function form($instance) {
//widgetform in backend
$name = $instance['title'];
$numberposts = $instance['numberposts'];
$catid = $instance['catid'];
$rss = $instance['rss'];
?>
<?php } // end of the instance
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['catid'] = $new_instance['catid'];
$instance['numberposts'] = $new_instance['numberposts'];
$instance['title'] = $new_instance['title'];
$instance['rss'] = $new_instance['rss'];
return $instance;
} // end the updated
function widget( $args, $instance ) {
extract($args, EXTR_SKIP);
$title = $instance['title'];
$catid = $instance['catid'];
$numberposts = $instance['numberposts'];
$posts = get_posts('numberposts='.$numberposts.'&category='.$catid);
$out = '<ul>';
foreach($posts as $post) {
$out .= '<li><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>';
}
$out .= '</ul>';
echo $before_widget;
echo $before_title.$title.$after_title;
echo $out;
echo $after_widget;
} // end of widget function
} // end the whole process ready to register
register_widget('habitat_posts');
When I try to use it on the child theme here everything goes OK except the Widget area in the backend is a jumbled up mess. The new widget is visible and the form fields for creating a new instance are visible and I can drag it to a sidebar but that is all.
Thanks for any suggestions or help Ernie