- 21, Oct 2024
- #1
Я хочу использовать wp_get_attachment_image() или wp_get_attachment_image_src(). Но этот виджет не принадлежит ни одному типу сообщений.
<img src="<?php echo esc_url_raw( $instance['single_feature_image_uri'] ); ?>" alt="">
Image instance is wp_get_attachment_image_src()
wp_get_attachment_image()
Как я могу использовать public function widget( $args, $instance ) {
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : '';
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
if ( empty( $instance['title'] ) ) {
return;
}
echo $args['before_widget']; ?>
<div class="single-feature__left">
<div class="single-feature__photo"></div>
<div class="single-feature__left-container content-wrapper">
<h2 class="single-feature__title"><?php echo $title; ?></h2>
<?php if ( ! empty( $instance['text']) ) : ?>
<?php echo esc_textarea( $instance['text'] ); ?>
<?php endif; ?>
<?php if ( ! empty( $instance['single_feature_cta_text'] ) ) : ?>
<?php $single_feature_cta_link = ( ! empty( $instance['single_feature_cta_link'] ) ) ? 'href="'. esc_url_raw( $instance['single_feature_cta_link'] ) .'"' : ''; ?>
<a class="single-feature__btn btn" <?php echo $single_feature_cta_link; ?>><?php esc_attr_e( $instance['single_feature_cta_text'] ); ?></a>
<?php endif; ?>
</div>
</div>
<div class="single-feature__right">
<?php if ( ! empty( $instance['single_feature_image_uri'] ) ) : ?>
<img src="<?php echo esc_url_raw( $instance['single_feature_image_uri'] ); ?>" alt="Художественное фото 1 справа" class="single-feature__photo">
<?php endif; ?>
</div>
<?php echo $args['after_widget'];
}
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$text = isset( $instance['text'] ) ? esc_html( $instance['text'] ) : '';
$single_feature_cta_link = isset( $instance['single_feature_cta_link'] ) ? esc_url_raw( $instance['single_feature_cta_link'] ) : '';
$single_feature_cta_text = isset( $instance['single_feature_cta_text'] ) ? esc_html( $instance['single_feature_cta_text'] ) : '';
$single_feature_image_uri = isset( $instance['single_feature_image_uri'] ) ? esc_url( $instance['single_feature_image_uri'] ) : '';
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title: ' ) ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $title; ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Your Text Field: ' ); ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>" value="<?php echo $text; ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'single_feature_cta_link' ); ?>"><?php _e( 'CTA button Link: ' ); ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'single_feature_cta_link' ); ?>" name="<?php echo $this->get_field_name( 'single_feature_cta_link' ); ?>" value="<?php echo $single_feature_cta_link; ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'single_feature_cta_text' ); ?>"><?php _e( 'Button text: ' ); ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'single_feature_cta_text' ); ?>" name="<?php echo $this->get_field_name( 'single_feature_cta_text' ); ?>" value="<?php echo $single_feature_cta_text; ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('single_feature_image_uri'); ?>"><?php _e( 'Image' ); ?></label>
<img src="<?php echo (! empty( $instance['single_feature_image_uri'] )) ? $instance['single_feature_image_uri'] : '' ?>" alt="" class="<?php echo $this->id; ?>_img" style="margin:0;padding:0;max-width:100%;display:block;">
<input type="text" class="widefat <?php echo $this->id; ?>_url" name="<?php echo $this->get_field_name( 'single_feature_image_uri' ); ?>" value="<?php echo $single_feature_image_uri; ?>" style="margin-top:5px;">
<input type="button" id="<?php echo $this->id; ?>" class="button button-primary js_custom_upload_media" value="<?php _e( 'Upload Image' );?>" style="margin-top:5px;">
</p>
<?php
}
or single_feature_image_uri
вместо widget()
Спасибо.
#php #разработка тем #виджеты