我正在尝试为我的网站创建一个小部件。这是我第一次创建widget,我真的不确定问题是什么
以下是我的代码:
add_action( \'widgets_init\', \'wy_rss_widget\' );
function wy_rss_widget() {
register_widget( \'WY_rss_widget\' );
}
class WY_rss_widget extends WP_Widget {
function WY_rss_widget() {
$widget_ops = array( \'classname\' => \'wy-rss\', \'description\' => __(\'A widget for displaying RSS feed\', \'wy-rss\') );
$control_ops = array( \'width\' => 300, \'height\' => 350, \'id_base\' => \'wy-rss-widget\' );
$this->WP_Widget( \'wy-rss-widget\', __(\'RSS Widget\', \'wy-rss\'), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args );
//Our variables from the widget settings.
$title = apply_filters(\'widget_title\', $instance[\'title\'] );
$wy_rssURL = $instance[\'wy_rssURL\'];
$wy_rssLimit = $instance[\'wy_rssLimit\'];
echo $before_widget;
// Display the widget title
if ( $title ) {
echo $before_title . $title . $after_title;
}
//Display the name
if ( ( $wy_rssURL != " " || $wy_rssURL != "FEED URL" ) && ( $wy_rssLimit != " " ) ) {
$rss = new DOMDocument();
$rss->load($wy_rssURL);
$feed = array();
foreach ($rss->getElementsByTagName(\'item\') as $node) {
$item = array (
\'title\' => $node->getElementsByTagName(\'title\')->item(0)->nodeValue,
\'desc\' => $node->getElementsByTagName(\'description\')->item(0)->nodeValue,
\'link\' => $node->getElementsByTagName(\'link\')->item(0)->nodeValue,
\'date\' => $node->getElementsByTagName(\'pubDate\')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = $wy_rssLimit;
echo \'<ul>\';
echo \'<li>\'.$wy_rssURL.\' (\'.$limit.\')</li>\';
for($x=0;$x<$limit;$x++) {
$title = str_replace(\' & \', \' & \', $feed[$x][\'title\']);
$link = $feed[$x][\'link\'];
$description = $feed[$x][\'desc\'];
$date = date(\'l F d, Y\', strtotime($feed[$x][\'date\']));
echo \'<li>\';
echo \'<p><strong><a href="\'.$link.\'" title="\'.$title.\'">\'.$title.\'</a></strong><br />\';
echo \'<small><em>Posted on \'.$date.\'</em></small></p>\';
echo \'<p>\'.$description.\'</p>\';
echo \'</li>\';
}
echo \'</ul>\';
}
echo $after_widget;
}
//Update the widget
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
//Strip tags from title and name to remove HTML
$instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
$instance[\'wy_rssURL\'] = strip_tags( $new_instance[\'wy_rssURL\'] );
$instance[\'wy_rssLimit\'] = strip_tags( $new_instance[\'wy_rssLimit\'] );
return $instance;
}
function form( $instance ) {
//Set up some default widget settings.
$defaults = array( \'title\' => __(\'Title\', \'wy-rss\'), \'wy_rssURL\' => __(\'FEED URL\', \'wy-rss\'), \'wy_rssLimit\' => __(\'5\',\'wy-rss\') );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<p>
<label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e(\'Title:\', \'wp-rss\'); ?></label>
<input id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo $instance[\'title\']; ?>" style="width:100%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id( \'wy_rssURL\' ); ?>"><?php _e(\'URL:\', \'wp-rss\'); ?></label>
<input id="<?php echo $this->get_field_id( \'wy_rssURL\' ); ?>" name="<?php echo $this->get_field_name( \'wy_rssURL\' ); ?>" value="<?php echo $instance[\'wy_rssURL\']; ?>" style="width:100%;" />
</p>
<p>
<label for"<?php echo $this->get_field_id( \'wy_rssLimit\' ); ?>"><?php _e(\'Number of items:\', \'wp-rss\'); ?></label>
<input id="<?php echo $this->get_field_id( \'wy_rssLimit\' ); ?>" name="<?php echo $this->get_field_id( \'wy_rssLimit\' ); ?>" value="<?php echo $instance[\'wy_rssLimit\']; ?>" />
</p>
<?php
}
}
未存储“wy\\u rssLimit”的值,其中“title”和“wy\\u rssURL”的值为。我真的不知道该去哪里找。有人能帮忙吗?