我尝试创建一个简单的小部件来显示特定类别的帖子列表,我在"Notice: Undefined index: after_widget in"
中收到错误日志
我还注意到一个奇怪的事情,我的widget <aside>
类正在使所有其他小部件成为它的子节点,当小部件代码完成时她没有关闭,
这是我的代码:
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
&#13;
似乎无法弄清楚,我必须说我的PHP非常有限。
由于
答案 0 :(得分:5)
您的错误日志会告诉您一个名为&#39; after_widget&#39;的未定义索引。
错误消息中的代码行丢失了,但我认为失败的原因在于以下行:
echo $args['after_widget'];
$args
是作为函数widget($args...
的参数给出的数组
在您的<ul>
- 部分中,您将使用以下内容覆盖它:
$args = array(
'cat' => $instance["cat"]
);
这就是为什么声明echo $args['after_widget'];
无法引用索引&after -widget&#39;然后导致丢失的元素。
尝试重命名自制的$args
变量,在其中实例化cat
数组,你应该好好去。