你的小部件确实有很多问题,完全破坏了我在后端小部件页面上的布局。我建议你从Widget API 学习编写小部件的正确方法
以下是我发现的几个问题:
声誉良好的消息来源不应建议甚至使用extract()
. 该函数的所有痕迹都已从core中完全删除,只有一个实例(据我从trac记录单中了解)尚未解决。这应该说明了很多关于函数的内容
您的小部件form
是破坏我布局的罪魁祸首。我甚至没有试着去看这个问题,因为这里有两个以上的问题。我刚刚完全改变了这一点get_categories()
正如我之前建议的那样,让下拉菜单正常工作并阻止我的布局被破坏
您的widget
应该输出前端信息的函数缺少许多元素。我不确定你想要的是什么样的布局,也不确定你的计划在使用中是什么,所以我只做了以下几点:(这是你应该拥有的基本必需品)
?><section id="<?php echo $sectionID; ?>"><?php
echo $args[\'before_widget\'];
if ( $title ) {
echo $args[\'before_title\'] . $title . $args[\'after_title\'];
}
echo "SOMETHING IS MISSING HERE";
echo $args[\'after_widget\'];
?></section><?php
根据
naming convention section in the coding standards我还在小部件中添加了一个缓存以加快速度
下面是一个小部件的工作示例。我已经测试过了,它对我来说很有效。我还将在前端获得给定代码的输出,您只需修改此代码并在我声明的地方添加缺少的部分echo "SOMETHING IS MISSING HERE";
/**
* Alliance_Post_Builder widget class
*
* @since 1.0.0
*/
class Alliance_Post_Builder extends WP_Widget {
public function __construct() {
parent::__construct(
\'alliance_post_builder\', // Base ID
__( \'Alliance post builder\', \'alliance\' ), // Name
array( \'description\' => __( \'A post builder\', \'alliance\' ), ) // Args
);
$this->alt_option_name = \'widget_alliance_posts\';
add_action( \'save_post\', array($this, \'flush_widget_cache\') );
add_action( \'deleted_post\', array($this, \'flush_widget_cache\') );
add_action( \'switch_theme\', array($this, \'flush_widget_cache\') );
}
public function widget( $args, $instance ) {
$cache = array();
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( \'widget_alliance_build_posts\', \'widget\' );
}
if ( ! is_array( $cache ) ) {
$cache = array();
}
if ( ! isset( $args[\'widget_id\'] ) ) {
$args[\'widget_id\'] = $this->id;
}
if ( isset( $cache[ $args[\'widget_id\'] ] ) ) {
echo $cache[ $args[\'widget_id\'] ];
return;
}
ob_start();
$title = ( ! empty( $instance[\'title\'] ) ) ? $instance[\'title\'] : __( \'A new section\', \'alliance\' );
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( \'widget_title\', $title, $instance, $this->id_base );
$sectionID = ( ! empty( $instance[\'sectionID\'] ) ) ? $instance[\'sectionID\'] : __( \'A new section\', \'alliance\' );
$category = $instance[\'category\'];
?><section id="<?php echo $sectionID; ?>"><?php
echo $args[\'before_widget\'];
if ( $title ) {
echo $args[\'before_title\'] . $title . $args[\'after_title\'];
}
echo "SOMETHING IS MISSING HERE";
echo $args[\'after_widget\'];
?></section><?php
if ( ! $this->is_preview() ) {
$cache[ $args[\'widget_id\'] ] = ob_get_flush();
wp_cache_set( \'widget_alliance_build_posts\', $cache, \'widget\' );
} else {
ob_end_flush();
}
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
$instance[\'sectionID\'] = strip_tags( $new_instance[\'sectionID\'] );
$instance[\'category\'] = $new_instance[\'category\'];
$this->flush_widget_cache();
$alloptions = wp_cache_get( \'alloptions\', \'options\' );
if ( isset($alloptions[\'widget_alliance_posts\']) )
delete_option(\'widget_alliance_posts\');
return $instance;
}
public function flush_widget_cache() {
wp_cache_delete(\'widget_alliance_build_posts\', \'widget\');
}
public function form( $instance ) {
$title = isset( $instance[\'title\'] ) ? esc_attr( $instance[\'title\'] ) : \'\';
$sectionID = isset( $instance[\'sectionID\'] ) ? esc_attr( $instance[\'sectionID\'] ) : \'\';
$category = isset( $instance[\'category\'] ) ? $instance[\'category\'] : -1;
?>
<p>
<label for="<?php echo $this->get_field_id( \'sectionID\' ); ?>"><?php _e( \'Section Id:\' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( \'sectionID\' ); ?>" name="<?php echo $this->get_field_name( \'sectionID\' ); ?>" type="text" value="<?php echo esc_attr( $sectionID ); ?>">
</p>
<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 $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id(\'category\'); ?>"><?php _e( \'Category Name:\' )?></label>
<select id="<?php echo $this->get_field_id(\'category\'); ?>" name="<?php echo $this->get_field_name(\'category\'); ?>">
<?php
$this->categories = get_categories();
foreach ( $this->categories as $cat ) {
$selected = ( $cat->term_id == esc_attr( $category ) ) ? \' selected = "selected" \' : \'\';
$option = \'<option \'.$selected .\'value="\' . $cat->term_id;
$option = $option .\'">\';
$option = $option .$cat->name;
$option = $option .\'</option>\';
echo $option;
}
?>
</select>
</p>
<?php
}
}
add_action( \'widgets_init\', function () {
register_widget( \'Alliance_Post_Builder\' );
});