我有以下代码来显示定制器中的小部件。
function arphabet_widgets_init() {
register_sidebar( array(
\'name\' => \'Social Media button\',
\'id\' => \'smb\',
) );
register_sidebar( array(
\'name\' => \'Link button\',
\'id\' => \'lb\',
) );
class My_Widget extends WP_Widget {
public function __construct() {
$widget_ops = array(
\'classname\' => \'my_widget\',
\'description\' => \'Adds a new Social Media button\',
);
parent::__construct( \'my_widget\', \'Social Media button\', $widget_ops );
}
public function widget( $args, $instance ) {
echo $args[\'before_widget\'];
if ( ! empty( $instance[\'title\'] ) ) {
echo $args[\'before_title\'] . apply_filters( \'widget_title\', $instance[\'title\'] ) . $args[\'after_title\'];
}
if ( ! empty( $instance[\'url\'] ) ) {
echo $args[\'url_title\'] . apply_filters( \'widget_url\', $instance[\'url\'] ) . $args[\'after_url\'];
}
if ( ! empty( $instance[\'icon\'] ) ) {
echo $args[\'icon_title\'] . apply_filters( \'widget_icon\', $instance[\'icon\'] ) . $args[\'after_icon\'];
}
echo $args[\'after_widget\'];
}
public function form( $instance ) {
$title = ! empty( $instance[\'title\'] ) ? $instance[\'title\'] : __( \'New title\', \'text_domain\' );
$url = ! empty( $instance[\'url\'] ) ? $instance[\'url\'] : __( \'http://\', \'text_domain\' );
$icon = ! empty( $instance[\'icon\'] ) ? $instance[\'icon\'] : __( \'globe\', \'text_domain\' );
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( \'title\' ) ); ?>"><?php _e( esc_attr( \'Title: (for search engines)\' ) ); ?></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>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( \'url\' ) ); ?>"><?php _e( esc_attr( \'URL:\' ) ); ?></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>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( \'icon\' ) ); ?>"><?php _e( esc_attr( \'Icon:\' ) ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( \'icon\' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( \'icon\' ) ); ?>" type="text" value="<?php echo esc_attr( $icon ); ?>">
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance[\'title\'] = ( ! empty( $new_instance[\'title\'] ) ) ? strip_tags( $new_instance[\'title\'] ) : \'\';
$instance[\'url\'] = ( ! empty( $new_instance[\'url\'] ) ) ? strip_tags( $new_instance[\'url\'] ) : \'\';
$instance[\'icon\'] = ( ! empty( $new_instance[\'icon\'] ) ) ? strip_tags( $new_instance[\'icon\'] ) : \'\';
return $instance;
}
}
dynamic_sidebar( \'smb\' );
dynamic_sidebar( \'lb\' );
register_widget( \'My_Widget\' );
}
add_action( \'widgets_init\', \'arphabet_widgets_init\' );
这是可行的,但我不确定如何输出它们。这就是我所尝试的:
<?php if ( is_active_sidebar( \'smb\' ) ) { ?>
<h1><?php dynamic_sidebar( \'smb\' ); ?></h1>
<?php } ?>
但这会输出整个小部件。我想输出一个特定字段,例如标题或URL。
我希望它像这样输出:
<li><a href="[URL]" class="fa fa-[ICON]">[TITLE]</a></li>
最合适的回答,由SO网友:Kim Vinberg 整理而成
看看这个,它可能会引导你朝着你需要的方向前进:
首先:
register_sidebar( array(
\'name\' => \'Social Media button\',
\'id\' => \'smb\',
\'before_widget\' => \'<li>\',
\'after_widget\' => \'</li>\',
) );
第二个代码:
public function widget( $args, $instance ) {
$output = $args[\'before_widget\'];
$output .= \'<a href="\' . $instance[\'url\'] . \'" class="fa fa-\' .
$args[\'icon_title\'] . \'">\' . $instance[\'title\'] . \'</a>\';
$output .= $args[\'after_widget\'];
echo $output; // <li><a href="[URL]" class="fa fa-[ICON]">[TITLE]</a></li>
}
这些更改的输出将是:
<h1><li><a href="http://url.com" class="fa fa-">New title</a></li><li><h2 class="widgettitle">test1</h2>
<div class="textwidget">test2</div>
</li></h1>
只会在社交侧边栏上这样做。