未与所有小部件一起显示的小部件的包装器div

时间:2016-05-12 作者:FullContactCoder

我已使用以下代码注册了一个小部件区域:

// Register Sidebars
function custom_sidebars() {

    $args = array(
        \'id\'            => \'footer_sidebar\',
        \'class\'         => \'footer_sidebar\',
        \'name\'          => __( \'Footer 3 foto ruimte\', \'text_domain\' ),
        \'description\'   => __( \'Widget ruimte voor 3 1 foto plaatje widgets\', \'text_domain\' ),
        \'before_widget\' => \'<div class="col-lg-4 col-md-4 col-sm-12 singleWidgetBlock">\',
        \'after_widget\' => \'</div>\'
    );
    register_sidebar( $args );

}
add_action( \'widgets_init\', \'custom_sidebars\' );
before\\u小部件和after\\u小部件与除一个以外的所有小部件一样正常工作。对于一个小部件,包装div根本不显示。

我在小部件代码本身中找不到任何可以阻止这种情况的东西。

还有其他人遇到过这个问题吗?或者有人知道会覆盖widgetized区域中的before\\u小部件和after\\u小部件代码吗?这次我的Google Fu严重不足。

为了完整起见,下面还提供了小部件本身的代码。

/**
 * Register the Widget
 */
add_action( \'widgets_init\', create_function( \'\', \'register_widget("fcc_single_image_widget");\' ) );

class fcc_single_image_widget extends WP_Widget
{
    /**
     * Constructor
     **/
    public function __construct()
    {
        $widget_ops = array(
            \'classname\' => \'fcc_single_image_widget\',
            \'description\' => \'Widget that uses the built in Media library.\'
        );

        parent::__construct( \'fcc_single_image_widget\', \'FCC Single Image Widget\', $widget_ops );

        add_action(\'admin_enqueue_scripts\', array($this, \'upload_scripts\'));
    }

    /**
     * Upload the Javascripts for the media uploader
     */
    public function upload_scripts()
    {
        wp_enqueue_script(\'media-upload\');
        wp_enqueue_script(\'thickbox\');
        wp_enqueue_script(\'single_image_widget\', plugin_dir_url(__FILE__) . \'upload-media.js\', array(\'jquery\'));

        wp_enqueue_style(\'thickbox\');
    }

    /**
     * Outputs the HTML for this widget.
     *
     * @param array  An array of standard parameters for widgets in this theme
     * @param array  An array of settings for this widget instance
     * @return void Echoes it\'s output
     **/
    public function widget( $args, $instance )
    {
        // Add any html to output the image in the $instance array

        ?>
        <h6 class="widget-title"><?php echo $instance[\'title\']?></h6>
        <img class="img-responsive" id="sidewidgetTopImage" src="<?php echo $instance[\'image\']; ?>">
        <?php
    }

    /**
     * Deals with the settings when they are saved by the admin. Here is
     * where any validation should be dealt with.
     *
     * @param array  An array of new settings as submitted by the admin
     * @param array  An array of the previous settings
     * @return array The validated and (if necessary) amended settings
     **/
    public function update( $new_instance, $old_instance ) {

        // update logic goes here
        $updated_instance = $new_instance;
        return $updated_instance;
    }

    /**
     * Displays the form for this widget on the Widgets page of the WP Admin area.
     *
     * @param array  An array of the current settings for this widget
     * @return void
     **/
    public function form( $instance )
    {
        $title = __(\'Widget Image\');
        if(isset($instance[\'title\']))
        {
            $title = $instance[\'title\'];
        }

        $image = \'\';
        if(isset($instance[\'image\']))
        {
            $image = $instance[\'image\'];
        }
        ?>
        <p>
            <label for="<?php echo $this->get_field_name( \'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>

        <p>
            <label for="<?php echo $this->get_field_name( \'image\' ); ?>"><?php _e( \'Image:\' ); ?></label>
            <input name="<?php echo $this->get_field_name( \'image\' ); ?>" id="<?php echo $this->get_field_id( \'image\' ); ?>" class="widefat" type="text" size="36"  value="<?php echo esc_url( $image ); ?>" />
            <input class="upload_image_button" type="button" value="Upload Image" />
        </p>
        <?php
    }
}
?>

1 个回复
最合适的回答,由SO网友:cjbj 整理而成

你没有回应包装师。register\\u侧栏仅定义div,但不呈现它。您需要提取文本并回显它们,如下所示:

public function widget( $args, $instance ) {    

    extract( $args );

    $title         = apply_filters( \'widget_title\', $instance[\'title\'] );

    echo $before_widget;

    if ( $title ) {
        echo $before_title . $title . $after_title;
    }

    ... do widgetty things ...

    echo $after_widget;

}

相关推荐

My widgets do not save

每次我保存我的小部件并离开页面时,我的小部件都会消失。侧边栏已完全清空,不会保存任何更改。控制台或PHP日志中没有任何错误。如果我将小部件直接复制并保存在数据库中widgets_text, 它们将被显示,但我仍然无法在侧边栏中添加或删除任何内容。这只发生在我的右侧边栏上,左侧边栏工作正常,但它们都以相同的方式注册。这是我注册侧边栏的方式:function my_widgets_init() { register_sidebar( array ( \'name\'