我目前正在接受创建小部件的培训,我的导师要求我创建一个文本小部件,然后添加在其上显示5条最新帖子(所有类别)的功能。我对如何添加它有点困惑,因为我对它很陌生。我会将下面的代码与url一起发布。我被推荐使用的是函数参考/wp从codex获取最近的帖子,但我对如何让它工作感到困惑。
<?php
/*
Plugin Name: Kevins Textbox
Plugin URI:
Description: A text widget created by Kevin Ullyott for practice in creating widgets
Author: Kevin Ullyott
Version: 1.0
Author URI:
*/
class kevintext extends WP_Widget {
public function __construct() {
parent::WP_Widget(
// or parent::__construct(
false,
\'Kevins Textbox\',
array(
\'description\' => __(\'A text widget created by Kevin Ullyott for practice in creating widgets\')
)
);
;
}
public function widget( $args, $instance ) {
extract( $args );
$headline = $instance[\'headline\'];
$text = $instance[\'text\'];
echo $before_widget;
echo $before_title;
echo "<p class=\\"headline\\">$headline</p>";
echo $after_title;
echo "<p class=\\"text\\">$text</p>";
echo $after_widget;
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance[\'headline\'] = ( $new_instance[\'headline\'] );
$instance[\'text\'] = ( $new_instance[\'text\'] );
return $instance;
}
public function form( $instance ) {
$headline = $instance[ \'headline\' ];
$text = $instance[ \'text\' ];
?>
<p>
<label for="<?php echo $this->get_field_id( \'headline\' ); ?>"><?php _e( \'Headline:\' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( \'headline\' ); ?>" name="<?php echo $this->get_field_name( \'headline\' ); ?>" type="text" value="<?php echo esc_attr( $headline ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( \'text\' ); ?>"><?php _e( \'Text:\' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( \'text\' ); ?>" name="<?php echo $this->get_field_name( \'text\' ); ?>" type="text" value="<?php echo esc_attr( $text ); ?>" />
</p>
<?php
}
}
add_action( \'widgets_init\', create_function(\'\', \'return register_widget("kevintext");\') );
?>
链接到url:
http://www.modmacro.us/wpsandbox/指向codex功能的链接:http://codex.wordpress.org/Function_Reference/wp_get_recent_posts