在Header.php中显示自定义微件值

时间:2013-11-20 作者:Manan

我将下面的代码用于自定义小部件。我在小部件中添加了一些字段,如:Title, Name 和复选框。现在一切都很好,但我想在标题中显示这些值。php。

有什么想法或建议吗?

add_action( \'widgets_init\', \'my_widget\' );


function my_widget() {
    register_widget( \'MY_Widget\' );
}

class MY_Widget extends WP_Widget {

    function MY_Widget() {
        $widget_ops = array( \'classname\' => \'example\', \'description\' => __(\'A widget that displays the authors name \', \'example\') );

        $control_ops = array( \'width\' => 300, \'height\' => 350, \'id_base\' => \'example-widget\' );

        $this->WP_Widget( \'example-widget\', __(\'Example Widget\', \'example\'), $widget_ops, $control_ops );
    }

    function widget( $args, $instance ) {
        extract( $args );

        //Our variables from the widget settings.
        $title = apply_filters(\'widget_title\', $instance[\'title\'] );
        $name = $instance[\'name\'];
        $show_info = isset( $instance[\'show_info\'] ) ? $instance[\'show_info\'] : false;

        echo $before_widget;

        // Display the widget title 
        if ( $title )
            echo $before_title . $title . $after_title;

        //Display the name 
        if ( $name )
            printf( \'<p>\' . __(\'Hey their Sailor! My name is %1$s.\', \'example\') . \'</p>\', $name );


        if ( $show_info )
            printf( $name );


        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[\'name\'] = strip_tags( $new_instance[\'name\'] );
        $instance[\'show_info\'] = $new_instance[\'show_info\'];

        return $instance;
    }


    function form( $instance ) {

        //Set up some default widget settings.
        $defaults = array( \'title\' => __(\'Example\', \'example\'), \'name\' => __(\'Bilal Shaheen\', \'example\'), \'show_info\' => true );
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>

        //Widget Title: Text Input.
        <p>
            <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e(\'Title:\', \'example\'); ?></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>

        //Text Input.
        <p>
            <label for="<?php echo $this->get_field_id( \'name\' ); ?>"><?php _e(\'Your Name:\', \'example\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'name\' ); ?>" name="<?php echo $this->get_field_name( \'name\' ); ?>" value="<?php echo $instance[\'name\']; ?>" style="width:100%;" />
        </p>


        //Checkbox.
        <p>
            <input class="checkbox" type="checkbox" <?php checked( $instance[\'show_info\'], true ); ?> id="<?php echo $this->get_field_id( \'show_info\' ); ?>" name="<?php echo $this->get_field_name( \'show_info\' ); ?>" /> 
            <label for="<?php echo $this->get_field_id( \'show_info\' ); ?>"><?php _e(\'Display info publicly?\', \'example\'); ?></label>
        </p>

    <?php
    }
}
?>

1 个回复
SO网友:Chip Bennett

抄本是你的朋友。有一个the_widget() 功能。

<?php the_widget( \'MY_Widget\', $instance, $args ); ?>
有关的值,请参阅Codex条目$instance$args.

$instance = array(
    \'title\' => \'Some Title here\',
    \'name\' => \'Some Name here\',
    \'show_info\' => true
);

$args = array(
    \'before_widget\' => \'\', // Your value here
    \'after_widget\' => \'\', // Your value here
    \'before_title\' => \'\', // Your value here
    \'after_title\' => \'\' // Your value here
)

结束

相关推荐

Post Templates and Post ID's

我正在尝试为单个帖子使用单个模板(我有一个自定义模板)。但由于某种原因,我的帖子被覆盖,而不是显示新数据。这是我现在的设置<div class=\"details\"> <?php global $post; $category_id=get_the_category($post->ID); $announcements = new WP_Query(); $announcements->query(\'showposts=1&a