admin管理员组文章数量:1122832
I am trying to make a custom widget. My codes are as follows:
class techno_widget extends WP_Widget {
function __construct() {
parent::__construct(
'techno_widget',
__('Recent Full Post', 'techno_widget_domain'),
array( 'description' => __( 'A full post will be appeared on Sidebar', 'techno_widget_domain' ), )
);
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
$blog-title = $instance['blog-title'];
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
echo $instance['blog-title'];
echo $args['after_widget'];
}
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_widget_domain' );
}
$blog-title = $instance[ 'blog-title' ];
?>
<p>
<label for="<?php echo $this->get_field_id( 'blog-title' ); ?>"><?php _e( 'Select Title:' ); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id( 'blog-title' ); ?>" name="<?php echo $this->get_field_name( 'blog-title' ); ?>">
<?php
$fullpost = new WP_Query(array(
'post_type' => 'post',
));
if($fullpost->have_posts()): while($fullpost->have_posts()): $fullpost->the_post(); ?>
<option value="<?php the_title();?>"><?php the_title();?></option>
<?php endwhile; endif;?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
function techno_load_widget() {
register_widget( 'techno_widget' );
}
add_action( 'widgets_init', 'techno_load_widget' );
Now wordpress show an error. It tells,
Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\wpexperiment\wp-content\themes\lawyer\functions.php on line 87
Whats wrong with my code? How can I get the value of my select box?
I am trying to make a custom widget. My codes are as follows:
class techno_widget extends WP_Widget {
function __construct() {
parent::__construct(
'techno_widget',
__('Recent Full Post', 'techno_widget_domain'),
array( 'description' => __( 'A full post will be appeared on Sidebar', 'techno_widget_domain' ), )
);
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
$blog-title = $instance['blog-title'];
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
echo $instance['blog-title'];
echo $args['after_widget'];
}
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_widget_domain' );
}
$blog-title = $instance[ 'blog-title' ];
?>
<p>
<label for="<?php echo $this->get_field_id( 'blog-title' ); ?>"><?php _e( 'Select Title:' ); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id( 'blog-title' ); ?>" name="<?php echo $this->get_field_name( 'blog-title' ); ?>">
<?php
$fullpost = new WP_Query(array(
'post_type' => 'post',
));
if($fullpost->have_posts()): while($fullpost->have_posts()): $fullpost->the_post(); ?>
<option value="<?php the_title();?>"><?php the_title();?></option>
<?php endwhile; endif;?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
function techno_load_widget() {
register_widget( 'techno_widget' );
}
add_action( 'widgets_init', 'techno_load_widget' );
Now wordpress show an error. It tells,
Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\wpexperiment\wp-content\themes\lawyer\functions.php on line 87
Whats wrong with my code? How can I get the value of my select box?
Share Improve this question asked Aug 31, 2016 at 5:07 Kallol DasKallol Das 1053 silver badges13 bronze badges1 Answer
Reset to default 0You missed the ending curly brace of the class techno_widget
, you need to call the function techno_load_widget
outside of the class techno_widget
and you declared the variable '$blog-titlewrong. You can't declare variable with
-. It is core
PHP` convention. So I rewrite your code like below-
class techno_widget extends WP_Widget {
function __construct() {
parent::__construct(
'techno_widget',
__('Recent Full Post', 'techno_widget_domain'),
array( 'description' => __( 'A full post will be appeared on Sidebar', 'techno_widget_domain' ), )
);
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
$blog_title = $instance['blog_title'];
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
echo $instance['blog_title'];
echo $args['after_widget'];
}
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_widget_domain' );
}
$blog_title = $instance[ 'blog_title' ];
?>
<p>
<label for="<?php echo $this->get_field_id( 'blog_title' ); ?>"><?php _e( 'Select Title:' ); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id( 'blog_title' ); ?>" name="<?php echo $this->get_field_name( 'blog_title' ); ?>">
<?php
$fullpost = new WP_Query(array(
'post_type' => 'post',
));
if($fullpost->have_posts()): while($fullpost->have_posts()): $fullpost->the_post(); ?>
<option value="<?php the_title();?>"><?php the_title();?></option>
<?php endwhile; endif;?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
function techno_load_widget() {
register_widget( 'techno_widget' );
}
add_action( 'widgets_init', 'techno_load_widget' );
And that was working. Here is the screenshot-
Now you can modify this code as you wish. But I think where you used $instance['blog-title']
(in my code that is $instance['blog_title']
) there may be you could use $instance['title']
.
And you can get the input as getting input form any other input fields. For further information please google it.
本文标签: how to get value of a select box in custom widget wordpress
版权声明:本文标题:how to get value of a select box in custom widget wordpress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736293023a1929058.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论