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

时间:2018-05-24 12:57:39

标签: php wordpress foreach widget counter

我尝试做一个自定义的wordpress小部件,女巫将显示插入的链接限制为6.我的问题是它只输出一个链接。我应该使用计数器还是foreach来实现我的目标?是否可以将此机制添加到public function form, public function updatepublic function widget,这样我就不必多次复制它并仅更改数字?

// 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' );

?>

0 个答案:

没有答案