Aka*_*rob 0 php wordpress widget wordpress-plugin
我试图创建一个简单的窗口小部件以显示特定类别的帖子列表,但出现错误日志,"Notice: Undefined index: after_widget in"
我还注意到一个奇怪的事情,我的窗口小部件<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_rollerRun Code Online (Sandbox Code Playgroud)
似乎无法弄清楚,我必须说我的PHP非常有限。
谢谢
您的错误日志会告诉您一个名为'after_widget'的未定义索引。
您的错误消息中的代码行丢失,但是我认为失败的原因在以下行中:
echo $args['after_widget'];
$args是作为函数的参数给出的数组。widget($args...
在您的<ul>-Part中,您将用以下命令覆盖它:
$args = array(
'cat' => $instance["cat"]
);
Run Code Online (Sandbox Code Playgroud)
这就是为什么该语句echo $args['after_widget'];无法引用索引“ after_widget” 的原因,该索引随后导致缺少元素。
尝试$args在实例化cat数组的地方重命名您的自制变量,您应该一切顺利。
| 归档时间: |
|
| 查看次数: |
646 次 |
| 最近记录: |