我用以下代码构建了一个自定义小部件:
class rfw_dock extends WP_Widget {
function __construct() {
parent::__construct(\'rfw_dock\', __(\'TITLE\'), array( \'description\' => __( \'DESCRIPTION\')));
}
function widget($args, $instance) {
$title = $instance[\'title\'];
$rss_url = $instance[\'rss_url\'];
$rss = fetch_feed($rss_url);
$rss_items = $rss->get_items( 0, 1 );
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
foreach ($rss_items as $item) {
echo \'<article><a href="\'. esc_url( $item->get_permalink() ) .\'" target="_blank" rel="nofollow"><h2>\'. $item->get_title() .\'</h2></a></article>\';
echo $after_widget;
}
}
function form($instance) {
$title = isset( $instance[\'title\'] ) ? esc_attr( $instance[\'title\'] ) : \'\';
$rss_url = isset( $instance[\'rss_url\'] ) ? esc_attr( $instance[\'rss_url\'] ) : \'\';
$number = isset( $instance[\'number\'] ) ? absint( $instance[\'number\'] ) : 6; ?>
<p><label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title (optional):\' ); ?></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>
<p><label for="<?php echo $this->get_field_id( \'rss_url\' ); ?>"><?php _e(\'Enter the RSS feed URL here:\'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( \'rss_url\' ); ?>" name="<?php echo $this->get_field_name( \'rss_url\' ); ?>" type="text" value="<?php echo esc_attr( $rss_url ); ?>" /></p><?php
}
function update($new_instance, $old_instance) {
$instance = array();
$instance[\'title\'] = strip_tags($new_instance[\'title\']);
$instance[\'rss_url\'] = strip_tags($new_instance[\'rss_url\']);
$instance[\'number\'] = (int) $new_instance[\'number\'];
$instance[\'show_feed_title\'] = ($new_instance[\'show_feed_title\']==true);
$instance[\'feed_words\'] = (int) $new_instance[\'feed_words\'];
$instance[\'content_display\'] = strip_tags($new_instance[\'content_display\']);
return $instance;
}
}
register_widget(\'rfw_dock\');`
并注册了此侧边栏:
register_sidebar( array(
\'name\' => __(\'Søstersider\'),
\'before_widget\' => \'<div>\',
\'after_widget\' => \'</div>\',
\'before_title\' => \'<h6>\',
\'after_title\' => \'</h6>\'
) );
很直接,对吧?我的问题是
$before_title
,
$after_title
,
$before_widget
和
$after_widget
不返回任何内容,即使它们在代码中指定。这些变量不是您应该如何定义register\\u侧栏中的信息吗?至少,它是在默认窗口小部件中。php。
很抱歉,虽然我认为自己在PHP方面有中等水平的知识,但我不是最好的。在发布这篇文章之前,我已经尝试了很多,但没有任何效果。有人能帮帮我吗?谢谢