I trying to create a simple widget to display a list of posts from a specific category, I'm getting an error log in "Notice: Undefined index: after_widget in"
I also noticed a weird thing that my widget <aside>
class is making all the other widgets be its child, she is not closing when the widget code is done,
This is my code:
if ( !defined('ABSPATH') )
die('-1');
add_action( 'widgets_init', function(){
register_widget( 'news_roller' );
});
function illu_news_widget(){
wp_enqueue_style( 'your-stylesheet-name', plugins_url('/css/style.css', __FILE__), false, '1.0.0', 'all');
}
add_action('wp_enqueue_scripts', "illu_news_widget");
/**
* Adds news_roller widget.
*/
class news_roller extends WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'news_roller', // Base ID
__('News roller', 'isas_news'), // Name
array( 'description' => __( 'News roller', 'isas_news' ), ) // 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);
// Excerpt length filter
function new_excerpt_length($length) {
return 10;
}
add_filter('excerpt_length', 'new_excerpt_length');
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
}
if ( ! empty( $instance['cat'] ) ) {
?>
<ul class="illu_widget news-carousel">
<?php
$args = array(
'cat' => $instance["cat"]
);
query_posts( $args );
while ( have_posts() ) : the_post();
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_excerpt(); ?>
</a>
</li>
<?php
endwhile;
wp_reset_query();
?>
</ul>
<?php
}
echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'isas-news' );
}
$cat = $instance['cat'];
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">
<?php _e( '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 esc_attr( $title ); ?>">
</p>
<label>
<?php _e( 'Category' ); ?>:
<?php wp_dropdown_categories( array(
'name' => $this->get_field_name("cat"),
'selected' => $instance["cat"] ) ); ?>
</label>
<?php
}
/**
* 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 = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['cat'] = ( ! empty( $new_instance['cat'] ) ) ? strip_tags( $new_instance['cat'] ) : '';
return $instance;
}
} // class news_roller
cant seem to figure it out, I'm must say my php is very limited.
Thanks
</div>
Your error log tells you about an undefined index named 'after_widget'.
The code line in your error message is missing, but i think the reason of failure is in following line:
echo $args['after_widget'];
$args
is an array given as parameter to the function widget($args...
In your <ul>
-Part you're overwriting it with:
$args = array(
'cat' => $instance["cat"]
);
That's why the statement echo $args['after_widget'];
can't reference to the index 'after_widget' which is then causing the missing Element.
Try to rename your self-made $args
variable where you instantiate the cat
array and you should be good to go.