Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在wordpress中添加我自己创建的小部件代码_Wordpress - Fatal编程技术网

在wordpress中添加我自己创建的小部件代码

在wordpress中添加我自己创建的小部件代码,wordpress,Wordpress,我用php程序为wordpress创建了我自己的小部件代码…我想把这个小部件添加到小部件面板中…所以如果我想在我的小部件面板中的那个小部件上显示我的小部件代码 <?php add_action( 'widgets_init', 'my_widget' ); function my_widget() { register_widget( 'MY_Widget' ); } class MY_Widget extends WP_Widget { function MY_

我用php程序为wordpress创建了我自己的小部件代码…我想把这个小部件添加到小部件面板中…所以如果我想在我的小部件面板中的那个小部件上显示我的小部件代码

<?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
    }
}

//小部件标题:文本输入。


您可以将其保存在主题的
functions.php
文件中,但最好将其保存在单独的文件中,然后将该文件包含在
functions.php

include('mywidget.php');

创建文件(
mywidget.php
)并将其保存在主题根文件夹(位于
functions.php
)中,然后将代码粘贴到此文件中,即是。将小部件代码保存在不同的文件中可以保持您的
函数。php
干净,并且您的小部件文件在自己的文件中可以很容易地读取/编辑,而无需在
函数中查看代码。php

您可以将其保存在主题的
函数.php
文件中,但如果您将其保存在单独的文件中并然后将文件包括在
functions.php
like中

include('mywidget.php');

创建文件(
mywidget.php
)并将其保存在主题根文件夹(位于
functions.php
)中,然后将代码粘贴到此文件中,即是。将小部件代码保存在不同的文件中将保持您的
函数.php
干净,并且您的小部件文件在它自己的文件中将很容易读取/编辑,而无需在
函数中查看代码.php

简易方法:在模板的
函数.php
简易方法:在模板的
函数.php