首页 > 解决方案 > 在自定义小部件中为变量添加值

问题描述

我试图做一个自定义的 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' );

?>

标签: phpwordpressforeachwidgetcounter

解决方案


推荐阅读