Php WordPress下拉多选选项
我有下面的WordPress下拉单选选项代码。 我想让这段代码与多重选择选项一起工作,但不知道如何。 请给出一个解决方案Php WordPress下拉多选选项,php,wordpress,select,multi-select,bootstrap-multiselect,Php,Wordpress,Select,Multi Select,Bootstrap Multiselect,我有下面的WordPress下拉单选选项代码。 我想让这段代码与多重选择选项一起工作,但不知道如何。 请给出一个解决方案 <?php $args=array( 'class' => 'select-submit2', 'hide_empty' => false, 'selected' => $prop_action_category_sel
<?php
$args=array(
'class' => 'select-submit2',
'hide_empty' => false,
'selected' => $prop_action_category_selected,
'name' => 'prop_action_category',
'id' => 'prop_action_category_submit',
'orderby' => 'NAME',
'order' => 'ASC',
'show_option_none' => __('None','wordpress'),
'taxonomy' => 'property_action_category',
'hierarchical'=> true
);
wp_dropdown_categories( $args ); ?>
<div class="ci-select">
<?php
wp_dropdown_categories( array(
'taxonomy' => 'property_location',
'hierarchical' => true,
'show_option_none' => esc_html_x( '-', 'any property location', 'ci_theme' ),
'option_none_value' => '',
'name' => 's_property_location',
'id' => 'property_location',
'selected' => isset( $_GET['s_property_location'] ) ? $_GET['s_property_location'] : '', // e.x 86,110,786
'multiple' => true
) );
?>
</div>
问候
<div class="ci-select">
<?php
wp_dropdown_categories( array(
'taxonomy' => 'property_location',
'hierarchical' => true,
'show_option_none' => esc_html_x( '-', 'any property location', 'ci_theme' ),
'option_none_value' => '',
'name' => 's_property_location',
'id' => 'property_location',
'selected' => isset( $_GET['s_property_location'] ) ? $_GET['s_property_location'] : '', // e.x 86,110,786
'multiple' => true
) );
?>
</div>
Nikoleta根据检查表,您需要使用wp_类别检查表。但是,如果您需要更脏的解决方案,此解决方案应该有帮助:
$dropdown = wp_dropdown_categories($args);
$dropdown = str_replace('id=', 'multiple="multiple" id=', $dropdown);
<div class="ci-select">
<?php
wp_dropdown_categories( array(
'taxonomy' => 'property_location',
'hierarchical' => true,
'show_option_none' => esc_html_x( '-', 'any property location', 'ci_theme' ),
'option_none_value' => '',
'name' => 's_property_location',
'id' => 'property_location',
'selected' => isset( $_GET['s_property_location'] ) ? $_GET['s_property_location'] : '', // e.x 86,110,786
'multiple' => true
) );
?>
</div>
wp_dropdown_categories函数是创建类别下拉列表的WordPress函数。您可以通过向函数传递各种参数来指定它的输出方式和内容
<div class="ci-select">
<?php
wp_dropdown_categories( array(
'taxonomy' => 'property_location',
'hierarchical' => true,
'show_option_none' => esc_html_x( '-', 'any property location', 'ci_theme' ),
'option_none_value' => '',
'name' => 's_property_location',
'id' => 'property_location',
'selected' => isset( $_GET['s_property_location'] ) ? $_GET['s_property_location'] : '', // e.x 86,110,786
'multiple' => true
) );
?>
</div>
但是,函数不接受将其从单选列表更改为多选列表的参数
<div class="ci-select">
<?php
wp_dropdown_categories( array(
'taxonomy' => 'property_location',
'hierarchical' => true,
'show_option_none' => esc_html_x( '-', 'any property location', 'ci_theme' ),
'option_none_value' => '',
'name' => 's_property_location',
'id' => 'property_location',
'selected' => isset( $_GET['s_property_location'] ) ? $_GET['s_property_location'] : '', // e.x 86,110,786
'multiple' => true
) );
?>
</div>
一种简单的方法是在函数生成后改变它给您的输出。这并不总是解决所有问题的最佳方法,但在这种情况下,有两个关键因素使这成为一种简单的方法:
<div class="ci-select">
<?php
wp_dropdown_categories( array(
'taxonomy' => 'property_location',
'hierarchical' => true,
'show_option_none' => esc_html_x( '-', 'any property location', 'ci_theme' ),
'option_none_value' => '',
'name' => 's_property_location',
'id' => 'property_location',
'selected' => isset( $_GET['s_property_location'] ) ? $_GET['s_property_location'] : '', // e.x 86,110,786
'multiple' => true
) );
?>
</div>
默认情况下,函数echoos打印其输出,但它接受一个参数,以简单地返回HTML结果,而不输出结果,从而允许您在显示结果之前对其进行更改。
将下拉列表更改为多选列表与将多选添加到HTML标记(即更改为)一样简单
您可以调用该函数,在输出结果之前将结果放入变量中,然后在该结果上使用PHP的str_replace将mulitple滑入select标记:
/**
* Your args from the question
* plus turning echo off.
* Note the change to the name (adding "[]")
*/
$args = array(
'class' => 'select-submit2',
'hide_empty' => false,
'selected' => $prop_action_category_selected,
'name' => 'prop_action_category[]',
'id' => 'prop_action_category_submit',
'orderby' => 'NAME',
'order' => 'ASC',
'show_option_none' => __('None','wpestate'),
'taxonomy' => 'property_action_category',
'hierarchical'=> true,
'echo' => 0,
);
/** get the dropdown **/
$dropdown = wp_dropdown_categories( $args );
/** insert "multiple" using str_replace **/
$multi = str_replace( '<select', '<select multiple ', $dropdown );
/** output result **/
echo $multi;
<div class="ci-select">
<?php
wp_dropdown_categories( array(
'taxonomy' => 'property_location',
'hierarchical' => true,
'show_option_none' => esc_html_x( '-', 'any property location', 'ci_theme' ),
'option_none_value' => '',
'name' => 's_property_location',
'id' => 'property_location',
'selected' => isset( $_GET['s_property_location'] ) ? $_GET['s_property_location'] : '', // e.x 86,110,786
'multiple' => true
) );
?>
</div>
传递echo参数0告诉函数不要输出任何内容,并根据需要向数组添加任何其他参数。然后对结果运行str_replace,结果就是您输出的结果
<div class="ci-select">
<?php
wp_dropdown_categories( array(
'taxonomy' => 'property_location',
'hierarchical' => true,
'show_option_none' => esc_html_x( '-', 'any property location', 'ci_theme' ),
'option_none_value' => '',
'name' => 's_property_location',
'id' => 'property_location',
'selected' => isset( $_GET['s_property_location'] ) ? $_GET['s_property_location'] : '', // e.x 86,110,786
'multiple' => true
) );
?>
</div>
请注意,您需要更改name参数来传递数组,以便能够传递/获取所有选定项。简单而有力地将此代码添加到functions.php文件中即可
<div class="ci-select">
<?php
wp_dropdown_categories( array(
'taxonomy' => 'property_location',
'hierarchical' => true,
'show_option_none' => esc_html_x( '-', 'any property location', 'ci_theme' ),
'option_none_value' => '',
'name' => 's_property_location',
'id' => 'property_location',
'selected' => isset( $_GET['s_property_location'] ) ? $_GET['s_property_location'] : '', // e.x 86,110,786
'multiple' => true
) );
?>
</div>
并添加多个参数,如下所示:
<div class="ci-select">
<?php
wp_dropdown_categories( array(
'taxonomy' => 'property_location',
'hierarchical' => true,
'show_option_none' => esc_html_x( '-', 'any property location', 'ci_theme' ),
'option_none_value' => '',
'name' => 's_property_location',
'id' => 'property_location',
'selected' => isset( $_GET['s_property_location'] ) ? $_GET['s_property_location'] : '', // e.x 86,110,786
'multiple' => true
) );
?>
</div>
从,您可以使用。你试过了吗?是的,我读过这篇文章,但无法处理,你能解释一下吗?谢谢你的帮助。函数默认会回显它的结果,所以你需要确保$args包含设置echo=0。例如-$dropdown=wp\u dropdown\u类别数组“echo”=>0;否则,结果将在str_replace运行之前输出。我使用下面的代码,它不会显示任何结果空的多选字段,您有点偏离了轨道。我编辑了答案中的示例,希望能够更清晰地添加注释和原始问题中的$args。谢谢,我尝试了在下拉列表中选择两个或更多值并按下提交按钮的示例,但不幸的是系统只保存一个值。这是一个好方法,但对于那些不知道你在做什么以及为什么要做的人来说,这需要更多的澄清/解释。