###前提・実現したいこと
複数の共通するウィジェットが存在するため、関数を利用して1つにまとめようとしています。ネストした関数内で引数を利用するにはどのように記述するのでしょうか?
###発生している問題・エラーメッセージ
引数に指定している$widgetName
が入れ子の関数内で利用できません。
初歩的な質問ですいません、、
###該当のソースコード
PHP
1createWidget('テスト'); 2 3function createWidget($widgetName){ 4 class other_list2 extends WP_Widget { 5 function other_list2() { 6 parent::WP_Widget(false, $name = $widgetName); 7 } 8 9 function widget($args, $instance) { 10 extract( $args ); 11 $slug = apply_filters( 'widget_slug', $instance['slug'] ); 12 $word = apply_filters( 'widget_word', $instance['word'] ); 13 ?> 14 15 <li <?php echo 'class="other_list"'; ?> > 16 <a href="<?php if ( $slug ) ?><?php echo $before_slug . $slug . $after_slug; ?>"> 17 <h5>' .$widgetName. '</h5> 18 <p>' . $word . '</p> 19 </a> 20 </li> 21 22 <?php 23 } 24 25 function update($new_instance, $old_instance) { 26 $instance = $old_instance; 27 $instance['slug'] = strip_tags($new_instance['slug']); 28 $instance['word'] = trim($new_instance['word']); 29 30 return $instance; 31 } 32 33 function form($instance) { 34 $slug = esc_attr($instance['slug']); 35 $word = esc_attr($instance['word']); 36 ?> 37 <p> 38 <label for="<?php echo $this->get_field_id('slug'); ?>"> 39 <?php _e('スラッグ'); ?> 40 </label> 41 <input class="widefat" id="<?php echo $this->get_field_id('slug'); ?>" name="<?php echo $this->get_field_name('slug'); ?>" type="text" value="<?php echo $slug; ?>" /> 42 </p> 43 44 <p> 45 <label for="<?php echo $this->get_field_id('word'); ?>"> 46 <?php _e('オススメの一言'); ?> 47 </label> 48 <textarea class="widefat" rows="4" id="<?php echo $this->get_field_id('word'); ?>" name="<?php echo $this->get_field_name('word'); ?>"> 49 <?php echo $word; ?> 50 </textarea> 51 </p> 52 <?php 53 } 54 } 55} 56 add_action('widgets_init', create_function('', 'return register_widget("other_list2");'));
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/18 13:51