0

在下面的代码中,我试图根据“widget”函数中的 $line_above 变量的值来更改 $widget_ops “classname”数组。我不确定如何寻址数组以更改值。

这可能吗?

class my_widget extends WP_Widget {

    function my_widget() {
        $widget_ops = array('classname' => 'test', 'description' => __( "test") );
        $control_ops = array('width' => 450, 'height' => 100, 'id_base' => 'my_widget' );
        $this->WP_Widget('my_widget', __('Test: test'), $widget_ops, $control_ops);
    }

    function widget( $args, $instance ) {
        extract($args);
        $title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance );
        $text = apply_filters( 'widget_text', $instance['text'], $instance );
        $line_above = isset( $instance['line_above'] ) ? $instance['line_above'] : false;

        if($line_above){
            //TRYING TO ALTER THE CLASS HERE BASED ON VARIABLE
            $widget_ops['classname'] .= " border-top";
        }
    }
}
4

1 回答 1

0

你可以让它全球化:

class my_widget extends WP_Widget {

    private $widget_ops;

    function my_widget() {
        $this->widget_ops = array('classname' => 'test', 'description' => __( "test") );
        $control_ops = array('width' => 450, 'height' => 100, 'id_base' => 'my_widget' );
        $this->WP_Widget('my_widget', __('Test: test'), $this->widget_ops, $control_ops);
    }

    function widget( $args, $instance ) {
        extract($args);
        $title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance );
        $text = apply_filters( 'widget_text', $instance['text'], $instance );
        $line_above = isset( $instance['line_above'] ) ? $instance['line_above'] : false;

        if($line_above){
            //TRYING TO ALTER THE CLASS HERE BASED ON VARIABLE
            $this->widget_ops['classname'] .= " border-top";
        }
    }
}
于 2013-02-04T19:46:49.683 回答