我创建了一个自定义小部件,其中一个字段是完整的电话号码(代码中的 fulltelephone)。它的用途是侧边栏的号召性用语小部件(例如输入数字、一些文本、按钮 URL 等)。有没有办法从这个字段中获取内容(如果它填充在小部件中)并将其添加到模板的某处?我想在页脚中添加一个电话号码,但不是在页脚中硬编码它,我认为如果它可以抓住已经在号召性用语小部件中输入的号码会很好。这是可行的吗?谢谢。
这是我的小部件代码:
add_action( 'widgets_init', 'sidebarctawidget' );
function sidebarctawidget() {
register_widget( 'sidebarctawidget' );
}
class sidebarctawidget extends WP_Widget {
function sidebarctawidget() {
$widget_ops = array( 'classname' => 'sidebarctawidget', 'description' => __('A widget for the sidebar call to actions ', 'sidebarctawidget') );
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'sidebarctawidget' );
$this->WP_Widget( 'sidebarctawidget', __('Sidebar Call to Action', 'sidebarctawidget'), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args );
//Our variables from the widget settings.
$title = apply_filters('widget_title', $instance['title'] );
$text = $instance['text'];
$prefix = $instance['prefix'];
$number = $instance['number'];
$fullnumber = $instance['fullnumber'];
$url = $instance['url'];
echo $before_widget;
echo '<div class="sidebarcta">';
// Display the title
if ( $title )
echo $before_title . $title . $after_title;
//Display the text
if ( $text )
printf( '<p class="text">' . __('%1$s', 'sidebarctawidget') . '</p>', $text );
//Display the text
if ( $prefix )
printf( '<span class="prefix">' . __('%1$s-', 'sidebarctawidget') . '</span>', $prefix );
//Display the text
if ( $number )
printf( '<span class="number">' . __('%1$s', 'sidebarctawidget') . '</span>', $number );
//Display the text
if ( $fullnumber )
printf( '<p class="fullnumber">' . __('(%1$s)', 'sidebarctawidget') . '</p>', $fullnumber );
//Display the button and URL
if ( $url )
printf( '<a class="button" href="%1$s">' . __('Learn More', 'sidebarctawidget') . '</a>', $url );
echo '</div>';
echo $after_widget;
}
//Update the widget
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
//Strip tags from title and name to remove HTML
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['text'] = strip_tags( $new_instance['text'] );
$instance['prefix'] = strip_tags( $new_instance['prefix'] );
$instance['number'] = strip_tags( $new_instance['number'] );
$instance['fullnumber'] = strip_tags( $new_instance['fullnumber'] );
$instance['url'] = strip_tags( $new_instance['url'] );
return $instance;
}
function form( $instance ) {
//Set up some default widget settings.
$defaults = array( 'title' => __('Get Connected TODAY!', 'sidebarctawidget') );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>" style="font-size: 12px; line-height: 16px;"><?php _e('Title:', 'sidebarctawidget'); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:97%; border: 1px solid #dfdfdf; -webkit-border-radius: 3px; border-radius: 3px;" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'text' ); ?>" style="font-size: 12px; line-height: 16px;"><?php _e('Text:', 'sidebarctawidget'); ?></label>
<input id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>" value="<?php echo $instance['text']; ?>" style="width:97%; border: 1px solid #dfdfdf; -webkit-border-radius: 3px; border-radius: 3px;" />
</p>
<p style="float: left;">
<label for="<?php echo $this->get_field_id( 'prefix' ); ?>" style="font-size: 12px; line-height: 16px;"><?php _e('Telephone Prefix:', 'sidebarctawidget'); ?></label>
<input id="<?php echo $this->get_field_id( 'prefix' ); ?>" name="<?php echo $this->get_field_name( 'prefix' ); ?>" value="<?php echo $instance['prefix']; ?>" style="width:50%; border: 1px solid #dfdfdf; -webkit-border-radius: 3px; border-radius: 3px; display: block;" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'number' ); ?>" style="font-size: 12px; line-height: 16px;"><?php _e('Telephone Number:', 'sidebarctawidget'); ?></label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $instance['number']; ?>" style="width:50%; border: 1px solid #dfdfdf; -webkit-border-radius: 3px; border-radius: 3px;" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'fullnumber' ); ?>" style="font-size: 12px; line-height: 16px;"><?php _e('Full Number:', 'sidebarctawidget'); ?></label>
<input id="<?php echo $this->get_field_id( 'fullnumber' ); ?>" name="<?php echo $this->get_field_name( 'fullnumber' ); ?>" value="<?php echo $instance['fullnumber']; ?>" style="width:97%; border: 1px solid #dfdfdf; -webkit-border-radius: 3px; border-radius: 3px;" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'url' ); ?>" style="font-size: 12px; line-height: 16px;"><?php _e('Button URL:', 'sidebarctawidget'); ?></label>
<input id="<?php echo $this->get_field_id( 'url' ); ?>" name="<?php echo $this->get_field_name( 'url' ); ?>" value="<?php echo $instance['url']; ?>" style="width:97%; border: 1px solid #dfdfdf; -webkit-border-radius: 3px; border-radius: 3px;" />
</p>
<?php
}
}