admin管理员组文章数量:1316329
I created my own category selection widget. So i used <select>
but i want to use with multiple
attribute.
Here is my widget class. It works perfect without multiple
attr. But i want to use it. And it must be return category ids in array.
<?php
class myCatWidget extends WP_Widget {
function myCatWidget() {
parent::WP_Widget( false, $name = 'My cat widget' );
}
function form( $instance ) {
$title = esc_attr( $instance[ 'title' ] );
$postCats = $instance[ 'postCats' ];
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title</label>
<input type="text" class="widfat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" style="width: 100%;" value="<?php echo $title; ?>"/>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'postCats' ); ?>">Categories</label>
<select name="<?php echo $this->get_field_name( 'postCats' ); ?>" id="<?php echo $this->get_field_id( 'postCats' ); ?>" style="width: 100%;" multiple>
<?php
$args = array(
'taxonomy' => 'category',
);
$terms = get_terms( $args );
foreach( $terms as $term ) { ?>
<option <?php selected( $instance[ 'postCats' ], $term->term_id ); ?> value="<?php echo esc_attr( $term->term_id ); ?>">
<?php echo esc_html( $term->name ); ?>
</option>
<?php } ?>
</select>
</p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
$instance[ 'postCats' ] = esc_sql( $new_instance[ 'postCats' ] );
return $instance;
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
$postCats = $instance[ 'postCats' ];
echo $before_widget;
if( $title ) {
echo $before_title . $title . $after_title;
}
echo $postCats;
echo $after_widget;
}
}
add_action( 'widgets_init', create_function( '', 'return register_widget("myCatWidget");' ) );
?>
I created my own category selection widget. So i used <select>
but i want to use with multiple
attribute.
Here is my widget class. It works perfect without multiple
attr. But i want to use it. And it must be return category ids in array.
<?php
class myCatWidget extends WP_Widget {
function myCatWidget() {
parent::WP_Widget( false, $name = 'My cat widget' );
}
function form( $instance ) {
$title = esc_attr( $instance[ 'title' ] );
$postCats = $instance[ 'postCats' ];
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title</label>
<input type="text" class="widfat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" style="width: 100%;" value="<?php echo $title; ?>"/>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'postCats' ); ?>">Categories</label>
<select name="<?php echo $this->get_field_name( 'postCats' ); ?>" id="<?php echo $this->get_field_id( 'postCats' ); ?>" style="width: 100%;" multiple>
<?php
$args = array(
'taxonomy' => 'category',
);
$terms = get_terms( $args );
foreach( $terms as $term ) { ?>
<option <?php selected( $instance[ 'postCats' ], $term->term_id ); ?> value="<?php echo esc_attr( $term->term_id ); ?>">
<?php echo esc_html( $term->name ); ?>
</option>
<?php } ?>
</select>
</p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
$instance[ 'postCats' ] = esc_sql( $new_instance[ 'postCats' ] );
return $instance;
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
$postCats = $instance[ 'postCats' ];
echo $before_widget;
if( $title ) {
echo $before_title . $title . $after_title;
}
echo $postCats;
echo $after_widget;
}
}
add_action( 'widgets_init', create_function( '', 'return register_widget("myCatWidget");' ) );
?>
Share
Improve this question
asked Aug 4, 2018 at 18:33
wpdevwpdev
5492 gold badges13 silver badges28 bronze badges
2 Answers
Reset to default 1Hi why don't you used multiple check-box instead of select option field. As well as you have used deprecated functions like "create_function". I have modified your code which replaced select option with multiple check-box selection.
function myCatWidget() {
register_widget( 'my_custom_cat_widget' );
}
add_action( 'widgets_init', 'myCatWidget' );
class my_custom_cat_widget extends WP_Widget {
function __construct() {
parent::__construct('my_custom_cat_widget', __('My cat widget', 'wpb_widget_domain'),
array( 'description' => __( 'Your description', 'wpb_widget_domain' ), )
);
}
public function form( $instance ) {
$title = isset($instance[ 'title' ]) ? $instance[ 'title' ] : 'Categories';
$instance['postCats'] = !empty($instance['postCats']) ? explode(",",$instance['postCats']) : array();
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title</label>
<input type="text" class="widfat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" style="width: 100%;" value="<?php echo $title; ?>"/>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'postCats' ); ?>"><?php _e( 'Select Categories you want to show:' ); ?></label><br />
<?php $args = array(
'post_type' => 'post',
'taxonomy' => 'category',
);
$terms = get_terms( $args );
//print_r($terms);
foreach( $terms as $id => $name ) {
$checked = "";
if(in_array($name->name,$instance['postCats'])){
$checked = "checked='checked'";
}
?>
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('postCats'); ?>" name="<?php echo $this->get_field_name('postCats[]'); ?>" value="<?php echo $name->name; ?>" <?php echo $checked; ?>/>
<label for="<?php echo $this->get_field_id('postCats'); ?>"><?php echo $name->name; ?></label><br />
<?php } ?>
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
$instance['postCats'] = !empty($new_instance['postCats']) ? implode(",",$new_instance['postCats']) : 0;
return $instance;
}
public function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
$postCats = $instance[ 'postCats' ];
$categories_list = explode(",", $postCats);
echo $before_widget;
if( $title ) {
echo $before_title . $title . $after_title;
}
$args = array('post_type' => 'post','taxonomy' => 'category',);
$terms = get_terms( $args );
?>
<ul>
<?php
foreach ($categories_list as $cat) {
foreach($terms as $term) {
if($cat === $term->name) {
echo "<li>".$term->name."</li>";
}
}
}
?>
</ul>
<?php
echo $after_widget;
}
}
Hope it will help to solved your problem. :)
Simple. Put two brackets right after the select name like
name="<?php echo $this->get_field_name( 'postCats' ); ?>"
will be named as
name="<?php echo $this->get_field_name( 'postCats' ); ?>[]"
The complete array of options will then be returned as you expect.
本文标签: categoriesMultiple selection for wordpress widget
版权声明:本文标题:categories - Multiple selection for wordpress widget 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741996936a2410231.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论