2

テーマ プラグイン ディレクトリにカスタム ウィジェットを作成しましたが、期待どおりに動作しているように見えますが、2 つ目のカスタム ウィジェットを登録すると、最初のウィジェットが上書きされてアクセスできなくなります。以下は、ウィジェットの私のコードです。

add_action( 'widgets_init', create_function( '', 'register_widget( "staffWidget" );' )     );


class staffWidget extends WP_Widget{
    function staffWidget() {  
            parent::WP_Widget(true, 'Staff');  
    } 

    function widget($args, $instance){
        echo "test widget";
    }

    function update($new_instance, $old_instance){
        return $new_instance;
    }

    function form($instance){
        $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
        if($instance['title']){
            $title = $instance['title'];
        }
        else{
            $title = "Add title here";
    }
    ?>
    <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input     class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
    <?php
    }
}

どちらのウィジェットもこの種のコード構造を持っていますが、クラス名が異なり、両方のウィジェットが WP ダッシュボードのプラグイン セクションでアクティブ化されています。どんな助けや提案も大歓迎です。前もって感謝します :)

4

1 に答える 1

1

WP_Widget間違ったパラメータでクラスを呼び出しています。

/**
 * PHP5 constructor
 *
 * @param string $id_base Optional Base ID for the widget, lower case,
 * if left empty a portion of the widget's class name will be used. Has to be unique.
 * @param string $name Name for the widget displayed on the configuration page.
 * @param array $widget_options Optional Passed to wp_register_sidebar_widget()
 *   - description: shown on the configuration page
 *   - classname
 * @param array $control_options Optional Passed to wp_register_widget_control()
 *   - width: required if more than 250px
 *   - height: currently not used but may be needed in the future
 */
function __construct( $id_base = false, $name, $widget_options = array(), $control_options = array() ) {

(デフォルトfalse値)またはを入力するstringと、機能します。したがって、ウィジェットのスタッフと別のスタッフがあるとすると、これは次のようになります。

parent::WP_Widget('staff', 'Staff', array(), array() ); 

parent::WP_Widget('stuff', 'Stuff', array(), array() ); 

あなたのコードはattribute_escape、非推奨のを使用しています。を有効WP_DEBUGにすると、警告が表示されます。とにかく、常にオンにして開発することをお勧めします。
これはすべて、例として悪いソースを使用していることを示しています。これはカスタムウィジェットに関する記事です。

于 2013-02-15T19:56:34.293 に答える