我正在使用wordpress搜索表单来优化当前搜索,我尝试做的是搜索搜索结果页面,并根据查询设置值。
到目前为止,我已经成功地使用单选下拉和单个复选框这样做 -
<!-- SINGLE SELECT -->
<select name="baths" class="form-control">
<?php if (isset($_GET['baths'])) {
$bths = $_GET['baths']; ?>
<option value="<?php echo $bths; ?>"><?php echo $bths; ?></option>
<?php } else { ?>
<option value="Any">Any</option>
<?php } ?>
<option value="Any">Any</option>
<option value="1">1+</option>
<option value="2">2+</option>
<option value="3">3+</option>
<option value="4">4+</option>
<option value="5">5+</option>
<option value="6">6+</option>
<option value="7">7+</option>
<option value="8">8+</option>
<option value="9">9+</option>
<option value="10">10+</option>
</select>
<!-- SINGLE CHECKBOX -->
<input type="checkbox" name="dogs" class="styled" value ="yes" <?php if (isset($_GET['dogs'])) { ?>checked<?php } ?>>
这是有效的,但对于多个值,它没有。这是我生成一组复选框以选择便利设施的功能 -
<?php
$amenity_array = array();
$id = get_query_var('site');
if (!empty($id)) {
$property_amenities = get_post_meta($id, 'imic_property_amenities', true);
global $imic_options;
foreach ($property_amenities as $properties_amenities_temp) {
if ($properties_amenities_temp != 'Not Selected') {
array_push($amenity_array, $properties_amenities_temp);
}
}
}
global $imic_options;
if (isset($imic_options['properties_amenities']) && count($imic_options['properties_amenities']) > 1) {
foreach ($imic_options['properties_amenities'] as $properties_amenities) {
$am_name = strtolower(str_replace(' ', '', $properties_amenities));
$check = '';
if (in_array($properties_amenities, $amenity_array)) {
$check = 'checked="checked"';
}
<!-- HERE I TRY TO FIND THE SELECTED CHECKBOXES AND CHECK THEM OFF -->
if (isset($_GET['p_am'])) {
$ams = $_GET['p_am'];
echo '<div class="checkbox"><input type="checkbox" name="p_am" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
} else {
echo '<div class="checkbox"><input type="checkbox" name="p_am" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
}
<!-- END ISSET -->
}
} else {
_e('There is no Properties Amenities', 'framework');
}
?>
对于多选下拉列表我使用bootstrap multiselect,所以在我的模板上代码看起来像这样 -
<select name="property_type[]" id="pt-multi" class="form-control multi-select2" multiple="multiple">
<?php
$terms = get_terms( "property-type", array( 'hide_empty' => 0 ) );
$count = count($terms);
if ( $count > 0 ){
echo "<option value='Any'>All</option>";
foreach ( $terms as $term ) {
echo "<option value='" . $term->slug . "'>" . $term->name . "</option>";
}
}
?>
</select>
在页面上它呈现为---
<select name="property_type[]" id="pt-multi" class="form-control multi-select2 iOSselect" multiple="multiple" style="display: none;">
<option value="Any">All</option>
<option value="co-op">Co-Op</option>
<option value="condo">Condo</option>
</select>
<div class="btn-group" style="width: 100%;">
<button type="button" class="multiselect dropdown-toggle btn btn-default form-control multi-select2" data-toggle="dropdown" title="Property Type" style="width: 100%; overflow: hidden; text-overflow: ellipsis;">
<span class="multiselect-selected-text">Property Type</span>
<b class="caret"></b></button>
<ul class="multiselect-container dropdown-menu pull-right">
<li class="multiselect-item multiselect-all">
<a tabindex="0" class="multiselect-all">
<label class="checkbox"><input type="checkbox" value="multiselect-all"> Select all</label>
</a>
</li>
<li>
<a tabindex="0"><label class="checkbox">
<input type="checkbox" value="Any"> All</label>
</a>
</li>
<li>
<a tabindex="0"><label class="checkbox"><input type="checkbox" value="co-op"> Co-Op</label>
</a>
</li>
<li>
<a tabindex="0"><label class="checkbox"><input type="checkbox" value="condo"> Condo</label>
</a>
</li>
</ul>
</div>
有什么想法吗?
更新
使用我的设施上方的代码复选框在页面上显示如此---
<div>
<label>amenities</label>
<div class="checkbox">
<input type="checkbox" name="p_am" class="styled" value="Doorman (Full Time)">
<label for="doorman(fulltime)">Doorman (Full Time)</label>
</div>
<div class="checkbox">
<input type="checkbox" name="p_am" class="styled" value="Doorman (Part Time)"><label for="doorman(parttime)">Doorman (Part Time)</label>
</div>
<div class="checkbox">
<input type="checkbox" name="p_am" class="styled" value="Laundry (In-Unit)"><label for="laundry(in-unit)">Laundry (In-Unit)</label>
</div>
<div class="checkbox">
<input type="checkbox" name="p_am" class="styled" value="Fitness Center"><label for="fitnesscenter">Fitness Center</label>
</div>
</div>
这适用于我的搜索功能,允许用户检查要包含在查询字符串中的设施 -
p_am=Doorman+%28Full+Time%29&p_am=Indoor+Pool
我的搜索功能很广泛,但与设施的相关部分 -
$amenities = isset($_GET['p_am'])?$_GET['p_am']:'';
$amenities = ($amenities == __('Any', 'framework')) ? '' : $amenities;
// .....
if (!empty($amenities)) {
array_push($meta_query,array(
'key' => 'imic_property_amenities',
'value' => $amenities,
'compare'=>'LIKE'
));
}
更新
关于设置bootstrap multiselect的下拉选项,我正在尝试使用以下代码设置选项 -
<?php
$taxonomyName = "city-type";
$parent_terms = get_terms( $taxonomyName, array( 'parent' => 0, 'orderby' => 'slug', 'hide_empty' => false ) );
echo "<select name='property_nhb[]' class='form-control multi-select' id='p-nhb' multiple>";
foreach ( $parent_terms as $pterm ) {
// echo "<option data-val='" . $pterm->slug . "' value='Any' " . $selected . ">Any</option>";
//Get the Child terms
$terms = get_terms( $taxonomyName, array( 'parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false ) );
foreach ( $terms as $term ) {
if(isset($_GET['property_nhb[]'])) {
$pnhb = $_GET['property_nhb'];
$selected = 'selected';
}
echo "<option data-val='" . $pterm->slug . "' value='" . $term->slug . "' " . $selected . " >" . $term->name . "</option>";
}
}
echo "</select>";
?>
我当前的搜索功能code
答案 0 :(得分:5)
这对于创建搜索表单似乎有点落后 - 看起来更像是为每个单独的结果生成表单?下面带注释的代码和注释。我在答案中试图保持你的编码风格。
$amenity_array = array();
$id = get_query_var('site');
// this first section gets the selected meta properties for the queried property (if available)
// [skipping for brevity ]
global $imic_options;
if (isset($imic_options['properties_amenities']) && count($imic_options['properties_amenities']) > 1) {
// this loops over all of the globally defined properties
foreach ($imic_options['properties_amenities'] as $properties_amenities) {
// gets the name of the property
$am_name = strtolower(str_replace(' ', '', $properties_amenities));
// and sets up to check the box if the property has the defined amenity
// if this is for a search form, why check boxes based on a result?
$check = '';
if (in_array($properties_amenities, $amenity_array)) {
// note: this only really needs to be 'checked'
$check = 'checked="checked"';
}
但是这里的东西有点交叉用途。
if (isset($_GET['p_am'])) {
$ams = $_GET['p_am'];
echo '<div class="checkbox"><input type="checkbox" name="p_am" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
} else {
echo '<div class="checkbox"><input type="checkbox" name="p_am" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
}
这表示所有复选框都具有相同的名称("p_am"
),而不是使用环境名称($am_name
)或某些表单组合(如果您希望所有设施位于同一搜索输入数组中) (例如"p_am[$am_name]"
)。
如果提供给$check
数组,每个复选框也会更改$_GET
的值。
$ams = $_GET['p_am'];
if (isset($ams[$am_name])) {
$check = 'checked';
}
每个复选框都有name="p_am['.$am_name.']"
作为名称。
echo '<div class="checkbox"><input type="checkbox" name="p_am[' . $am_name . ']" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
如果您希望每个设施都有唯一的名称(查看原始的复选框,根本不是p_am
)而不是PHP中的数组,那么您只需使用循环名称对于舒适性($am_name
),像这样:
if (isset($_GET[$am_name])) {
$check = 'checked';
}
每个复选框都有name="'.$am_name.'"
作为名称。
更新:,看起来每个复选框都需要具有相同的名称,但不是键控值。对于这种情况,您的复选框应该被称为p_am[]
(在搜索页面和原始页面上),您需要使用in_array()
而不是isset()
来检查结果,就像这样:
if (in_array($properties_amenities, $_GET['p_am'])) {
$check = 'checked';
}
附加说明 - 您还使用$am_name
作为标签,而没有在复选框上实际设置id
属性以匹配,而不是剥离非id
字符(如括号),因此标签关联不起作用。
echo '<div class="checkbox"><input type="checkbox" name="' . $am_name . '" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
对于bootstrap选择,您只是错过了检查选择了哪些选项:
<select name="property_type[]" id="pt-multi" class="form-control multi-select2" multiple="multiple">
<?php
$terms = get_terms( "property-type", array( 'hide_empty' => 0 ) );
$count = count($terms);
if ( $count > 0 ){
echo "<option value='Any'>All</option>";
foreach ( $terms as $term ) {
// if the option is 'checked', you need to add the 'selected' atttibute here
echo "<option value='" . $term->slug . "'>" . $term->name . "</option>";
}
}
?>
</select>
所以循环内部看起来就像这样:
// for efficiency's sake should live outside the loop as a one-off, but here for illustrative purposes
$types = $_GET['property_type'];
$selected = isset($types[$term->slug]) ? 'selected' : '';
echo "<option value='" . $term->slug . "'" . $selected . ">" . $term->name . "</option>";
如果您使用较旧版本的bootstrap select,则可能需要将所选字符串设为'selected="selected"
或data-selected="true"
,具体取决于您使用的版本。对于当前版本,上面的字符串应该没问题。
如果<select>
不是一个选项数组(即不是多个),那么应该从名称中删除[]
,并且操作方式与复选框代码类似:
<select name="property_type" id="pt-multi" class="form-control multi-select2">
<?php
$terms = get_terms( "property-type", array( 'hide_empty' => 0 ) );
$count = count($terms);
if ( $count > 0 ){
echo "<option value='Any'>All</option>";
foreach ( $terms as $term ) {
$type = $_GET['property_type'];
$selected = isset($type) && $type == $term-slug ? 'selected' : '';
echo "<option value='" . $term->slug . "'" . $selected . ">" . $term->name . "</option>";
}
}
?>
</select>
<select>
的原始HTML实际上并不“正常” - 所有它正在做的是在选项列表的顶部添加重复的<option>
,并且因为没有已被选中,表单会将在顶部创建的副本视为<select>
的“已选择”。
更新2:搜索功能因复选框输入数组而中断的原因是您使用元查询的LIKE
比较。来自the documentation on WordPress Meta Query:
只有当比较为“IN”,“NOT IN”,“BETWEEN”或“NOT BETWEEN”时,它才可以是数组。
该文档还显示了如何将这些连接在一起的示例。由于你没有提供你实际上如何进行搜索查询,这是猜测一点,但看起来像一个复选框数组,你的代码应该类似于:
if (!empty($amenities)) {
foreach ($amenities as $amenity) {
array_push($meta_query, array(
'key' => 'imic_property_amenities',
'value' => $amenity,
'compare' => 'LIKE'
));
}
}
与文档中的示例一样,您需要确保有relation => 'OR'
或relation => 'AND'
来定义搜索的运行方式。
答案 1 :(得分:2)
<击> 弗雷德在评论中的含义是这样的:
<input type="checkbox" value="condo" name="var_name[]">
name属性末尾的[]
告诉PHP它应该将变量视为数组。
然后在PHP脚本中,您可以使用类似以下内容检查数组长度:
击> <击> $arr = $_POST['var_name']; echo count($arr);
击>
基于leith的评论,如果它是自动生成的,那么你不应该打扰手动检查复选框。
相反,只需将所选选项设为selected
,例如:
<?php
$selections = $_GET['property_type']; // or something similar
?>
<select name="property_type[]" id="pt-multi" class="form-control multi-select2" multiple="multiple">
<?php
$terms = get_terms( "property-type", array( 'hide_empty' => 0 ) );
$count = count($terms);
if ( $count > 0 ){
echo "<option value='Any'>All</option>";
foreach ( $terms as $term ) {
$selected = in_array($term->slug, $selections);
echo "<option value='" . $term->slug . "' " . ($selected?' selected':'') . ">" . $term->name . "</option>";
}
}
?>
</select>
看到这个小提琴https://jsfiddle.net/u1w158tp/
其中只有selected
option
,它会自动检查checkbox
es
答案 2 :(得分:-1)
您的复选框名称相同。尝试添加不同的名称或使用“p_am []”将它们视为数组。