Php 向自定义小部件中的变量添加值

Php 向自定义小部件中的变量添加值,php,wordpress,foreach,widget,counter,Php,Wordpress,Foreach,Widget,Counter,我正在尝试做一个自定义wordpress小部件,它将显示最多6个插入链接。我的问题是它只输出一个链接。我应该使用计数器还是foreach来实现我的目标?是否可以将此机制也添加到公共功能表单、公共功能更新和公共功能小部件中,这样我就不必复制多次,只需更改数字 // The widget class class Custom_Link_Widget extends WP_Widget { // Main constructor public function __construct(

我正在尝试做一个自定义wordpress小部件,它将显示最多6个插入链接。我的问题是它只输出一个链接。我应该使用计数器还是foreach来实现我的目标?是否可以将此机制也添加到
公共功能表单、公共功能更新
公共功能小部件
中,这样我就不必复制多次,只需更改数字

// The widget class
class Custom_Link_Widget extends WP_Widget {
    // Main constructor
    public function __construct() {
        parent::__construct(
            'custom_link_widget',
            __( 'Custom Link Widget', 'text_domain' ),
            array(
                'customize_selective_refresh' => true,
            )
        );
    }

    // The widget form (for the backend )
    public function form( $instance ) {
      // Set widget defaults
      $defaults = array(
        'title'    => '',
        'name'     => '',
        'url'      => '',
            'target_type' => '',
      );

      // Parse current settings with defaults
      extract( wp_parse_args( ( array ) $instance, $defaults ) ); ?>

      <?php // Widget Title ?>
      <p>
        <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Tytuł Widgetu', 'text_domain' ); ?></label>
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
      </p>

      <?php // Name Field ?>
      <p>
        <label for="<?php echo esc_attr( $this->get_field_id( 'name' ) ); ?>"><?php _e( 'Nazwa:', 'text_domain' ); ?></label>
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'name' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'name' ) ); ?>" type="text" value="<?php echo esc_attr( $name ); ?>" />
      </p>

      <?php // URL Field ?>
      <p>
        <label for="<?php echo esc_attr( $this->get_field_id( 'url' ) ); ?>"><?php _e( 'Adres odnośnika:', 'text_domain' ); ?></label>
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'url' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'url' ) ); ?>" type="text" value="<?php echo esc_attr( $url ); ?>" />
      </p>

        <?php // Checkbox ?>
        <p>
            <input id="<?php echo esc_attr( $this->get_field_id( 'target_type' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'target_type' ) ); ?>" type="checkbox" value="1" <?php checked( '1', $target_type ); ?> />
            <label for="<?php echo esc_attr( $this->get_field_id( 'target_type' ) ); ?>"><?php _e( 'Link wychodzący', 'text_domain' ); ?></label>
        </p>

        <?php // 2 Name Field ?>
      <p>
        <label for="<?php echo esc_attr( $this->get_field_id( 'name2' ) ); ?>"><?php _e( 'Nazwa:', 'text_domain' ); ?></label>
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'name2' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'name2' ) ); ?>" type="text" value="<?php echo esc_attr( $name2 ); ?>" />
      </p>

      <?php // 2 URL Field ?>
      <p>
        <label for="<?php echo esc_attr( $this->get_field_id( 'url2' ) ); ?>"><?php _e( 'Adres odnośnika:', 'text_domain' ); ?></label>
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'url2' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'url2' ) ); ?>" type="text" value="<?php echo esc_attr( $url2 ); ?>" />
      </p>

        <?php // 2 Checkbox ?>
        <p>
            <input id="<?php echo esc_attr( $this->get_field_id( 'target_type2' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'target_type2' ) ); ?>" type="checkbox" value="1" <?php checked( '1', $target_type2 ); ?> />
            <label for="<?php echo esc_attr( $this->get_field_id( 'target_type2' ) ); ?>"><?php _e( 'Link wychodzący', 'text_domain' ); ?></label>
        </p>

    <?php }
    // Update widget settings
    public function update( $new_instance, $old_instance ) {
      $instance = $old_instance;
      $instance['title']    = isset( $new_instance['title'] ) ? wp_strip_all_tags( $new_instance['title'] ) : '';
      $instance['name']     = isset( $new_instance['name'] ) ? wp_strip_all_tags( $new_instance['name'] ) : '';
      $instance['url']      = isset( $new_instance['url'] ) ? wp_strip_all_tags( $new_instance['url'] ) : '';
        $instance['target_type'] = isset( $new_instance['target_type'] ) ? 1 : false;

        $instance['name2']     = isset( $new_instance['name2'] ) ? wp_strip_all_tags( $new_instance['name2'] ) : '';
      $instance['url2']      = isset( $new_instance['url2'] ) ? wp_strip_all_tags( $new_instance['url2'] ) : '';
      $instance['target_type2'] = isset( $new_instance['target_type2'] ) ? 1 : false;
      return $instance;
    }
    // Display the widget
    public function widget( $args, $instance ) {
      extract( $args );
      // Check the widget options
      $title         = isset( $instance['title'] ) ? apply_filters( 'widget_title', $instance['title'] ) : '';
      $name          = isset( $instance['name'] ) ? $instance['name'] : '';
      $url           = isset( $instance['url'] ) ? $instance['url'] : '';
        $target_type = ! empty( $instance['target_type'] ) ? $instance['target_type'] : false;
        $name        = isset( $instance['name2'] ) ? $instance['name2'] : '';
      $url           = isset( $instance['url2'] ) ? $instance['url2'] : '';
        $target_type = ! empty( $instance['target_type2'] ) ? $instance['target_type2'] : false;
      // WordPress core before_widget hook (always include )
      echo $before_widget;
      // Display the widget
        echo '<div class="widget-custom-links">';
          echo '<div class="widget-custom-links-wrapper">';
            // Display text field
                    echo '<ul>';

                        echo '<li>';
                            if ($target_type == 0) {
                                echo '<a href=" '. $url .' ">' . $name . '</a>';
                            }
                            else {
                                echo '<a href=" '. $url .' " target="_blank">' . $name . '</a>';
                            }
                        echo '</li>';

                    echo '</ul>';
          echo '</div>';
        echo '</div>';
      // WordPress core after_widget hook (always include)
      echo $after_widget;
    }
}

// Register the widget
function register_custom_link_widget() {
    register_widget( 'Custom_Link_Widget' );
}
add_action( 'widgets_init', 'register_custom_link_widget' );

?>
//小部件类
类自定义链接小部件扩展WP小部件{
//主建造师
公共函数构造(){
父项::u构造(
“自定义链接小部件”,
__(“自定义链接小部件”、“文本域”),
排列(
“自定义\u选择性\u刷新”=>true,
)
);
}
//小部件表单(用于后端)
公共函数表($instance){
//设置小部件默认值
$defaults=数组(
'标题'=>'',
'名称'=>'',
“url'=>”,
'目标类型'=>'',
);
//使用默认值分析当前设置
提取(wp_parse_args((数组)$instance$defaults));?>


您可以使用输入变量的名称,如=url[i],其中i是您的编号。因此,您可以使用循环,如for或while。然后在前端部分,您将使用名为$urlThank you@c0ns0l3的数组,感谢您的帮助:)