php - 在自定义小部件中为变量添加值
问题描述
我试图做一个自定义的 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' );
?>
解决方案
推荐阅读
- angularjs - 将 EEE 格式更改为另一种语言
- python - 我想问如何解决这个问题
- python - 如何在 SFTP 中使用 scrapy
- react-native - 由于连字符,React Native 从 REST API 获取将不起作用
- angular5 - 我需要从列表中拖动一个元素并将其放到角度 6 的另一个块中
- python - 如何通过特定的子/后代元素选择元素
- python-3.x - 无法将数据框绘制为 barh,因为 TypeError: Empty 'DataFrame': no numeric data to plot
- python - Python和硒:强制没有下载窗口
- xquery - MarkLogic 查询耗时过长
- python - Kivy:函数\方法不能从另一个类工作