добавление значения к переменной в пользовательском виджете

Я пытаюсь сделать собственный виджет WordPress, ведьма покажет вставленные ссылки с пределом 6. Моя проблема заключается в том, что он выводит только одну ссылку. Должен ли я использовать счетчик для этого или foreach для достижения моей цели? Можно ли добавить этот механизм также в public function form, public function update а также public 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

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector