我在表达引擎搜索方面遇到了一些麻烦!我有一个下拉式表单设置很好,但我需要在此搜索表单中添加一个OPTIONAL关键字字段。任何想法我将如何使用我当前的代码:
主要表单代码:
<form method="post" action="/properties/search-results/">
<p>Keywords:</p>
<input id="keywords" type="text" name="keywords"/>
<p>Town:</p>
<select id="town" name="cat[]" multiple="multiple">
<option value="" selected="selected">Any</option>
{exp:channel:categories channel="property" style="linear" category_group="1"}
<option value="{category_id}">{category_name}</option>
{/exp:channel:categories}
</select>
<p>Property Type:</p>
<select id="propertyType" name="cat[]">
<option value="" selected="selected">Any</option>
{exp:channel:categories channel="property" style="linear" category_group="2"}
<option value="{category_id}">{category_name}</option>
{/exp:channel:categories}
</select>
<input style="margin-top:20px; width: 100px;" type="submit" name="submit" value="Search"/>
</form>
搜索结果模板:
<?php
// Grab the categories selected from the $_POST
// join them with an ampersand - we are searching for AND matches
$cats = "";
foreach($_POST['cat'] as $cat){
// check we are working with a number
if(is_numeric($cat)){
$cats .= $cat."&";
}
}
// strip the last & off the category string
$cats = substr($cats,0,-1);
?>
{exp:channel:entries channel="property" dynamic="on" category="<?php echo($cats);?>" orderby="date" sort="asc"}
我需要关键字字段才能在我的参赛作品的{title}中搜索!
感谢您的帮助!
答案 0 :(得分:1)
试试这个:首先安装Search Fields插件。 (您需要这样做,因为本机EE "search:field_name"
参数仅适用于自定义字段,而不适用于条目标题。)
然后使用此修订代码:
<?php
// Grab the categories selected from the $_POST
// join them with an ampersand - we are searching for AND matches
$cats = array();
foreach($_POST['cat'] as $cat)
{
// check we are working with a number
if(is_numeric($cat))
{
$cats[] = $cat;
}
}
$cats = implode('&', $cats);
if(!empty($_POST['keywords']))
{
$keywords = trim($_POST['keywords']);
}
?>
<?php if($keywords) : ?>
{exp:search_fields search:title="<?php echo($keywords);?>" channel="property" parse="inward"}
<?php endif; ?>
{exp:channel:entries channel="property" <?php if($keywords) : ?>entry_id="{search_results}"<?php endif; ?> category="<?php echo($cats);?>" orderby="date" sort="asc"}
{!-- do stuff --}
{/exp:channel:entries}
<?php if($keywords) : ?>
{/exp:search_fields}
<?php endif; ?>