Creating a custom search form

时间:2014-10-01 作者:Ryan Bowden

我正在创建一个自定义搜索表单,它将搜索一个名为recipes的自定义帖子类型。它有大量的分类法。

现在,我将这些分类法的术语输出到按分类法名称分组的复选框中。

问题在于输入:

<!-- language: lang-html -->

  <label><input type="checkbox" name="epx_recipes_season[]" value="autumn">Autumn</label>
  <label><input type="checkbox" name="epx_recipes_season[]" value="spring">Spring</label>
  <label><input type="checkbox" name="epx_recipes_season[]" value="summer-3">Summer</label>
  <label><input type="checkbox" name="epx_recipes_season[]" value="winter-3">Winter</label>

  <label><input type="checkbox" name="epx_recipes_course[]" value="breakfast-3">Breakfast</label>
  <label><input type="checkbox" name="epx_recipes_course[]" value="brunch-2">Brunch</label>
  <label><input type="checkbox" name="epx_recipes_course[]" value="dessert">Dessert</label>
  <label><input type="checkbox" name="epx_recipes_course[]" value="dinner">Dinner</label>
  <label><input type="checkbox" name="epx_recipes_course[]" value="light-meals-2">Light Meals</label>
  <label><input type="checkbox" name="epx_recipes_course[]" value="lunch-3">Lunch</label>

  <label><input type="checkbox" name="epx_recipes_requirements[]" value="diary-free">Diary free</label>
  <label><input type="checkbox" name="epx_recipes_requirements[]" value="egg-free">Egg Free</label>
  <label><input type="checkbox" name="epx_recipes_requirements[]" value="gluten-free-3">Gluten Free</label>
  <label><input type="checkbox" name="epx_recipes_requirements[]" value="nut-free">Nut Free</label>
  <label><input type="checkbox" name="epx_recipes_requirements[]" value="shellfish-free">Shellfish free</label>
  <label><input type="checkbox" name="epx_recipes_requirements[]" value="vegitarians">vegitarians</label>

  <label><input type="checkbox" name="epx_recipes_cuisines[]" value="american">American</label>
  <label><input type="checkbox" name="epx_recipes_cuisines[]" value="british-3">British</label>
  <label><input type="checkbox" name="epx_recipes_cuisines[]" value="greek-3">Greek</label>
  <label><input type="checkbox" name="epx_recipes_cuisines[]" value="irish">Irish</label>
  <label><input type="checkbox" name="epx_recipes_cuisines[]" value="italian">Italian</label>
  <label><input type="checkbox" name="epx_recipes_cuisines[]" value="mexican-3">Mexican</label>
  <label><input type="checkbox" name="epx_recipes_occastions[]" value="birthdays">Birthdays</label>
  <label><input type="checkbox" name="epx_recipes_occastions[]" value="christmas">Christmas</label>
  <label><input type="checkbox" name="epx_recipes_occastions[]" value="easter-3">Easter</label>
  <label><input type="checkbox" name="epx_recipes_occastions[]" value="fathers-day-2">Fathers Day</label>
  <label><input type="checkbox" name="epx_recipes_occastions[]" value="mothers-day-2">Mothers day</label>
  <label><input type="checkbox" name="epx_recipes_occastions[]" value="valentines">Valentines</label>
问题来自于名称,他们必须将其分组在一起,他们有一个[],所以发布时它将作为数组,然后我可以解析它。这个问题是WordPress正在直接读取表单并输出以下错误消息。

注意:在/home/sites/epixdev1中进行数组到字符串的转换。co.uk/public\\u html/htk2014/wp-includes/query。php在线1858

警告:preg\\u split()要求参数2为字符串,数组在/home/sites/epixdev1中给出。co.uk/public\\u html/htk2014/wp-includes/query。php在线1867

注意:在/home/sites/epixdev1中进行数组到字符串的转换。co.uk/public\\u html/htk2014/wp-includes/query。php在线1858

警告:preg\\u split()要求参数2为字符串,数组在/home/sites/epixdev1中给出。co.uk/public\\u html/htk2014/wp-includes/query。php在线1867

注意:正在尝试获取/home/sites/epixdev1中非对象的属性。co.uk/public\\u html/htk2014/wp-content/plugins/wordpress-seo/frontend/class-opengraph。php在线524

下面是生成表单的代码

<!-- language: lang-php -->

//Will be used to output the advanced search box
function epix_recipes_search(){
    //Need to run each different type of taxonmies
    //How Many there are
    $noTax = 6;

    //The Taxonomies
    $taxonomies = array(\'epx_recipes_season\',\'epx_recipes_course\',\'epx_recipes_requirements\',\'epx_recipes_cuisines\',\'epx_recipes_occastions\',\'epx_recipes_typeofcooking\');

    //need the basic output varible 
    $output = "";

    //Need to build a basic form
    $output .= \'<form method="post" id="searchform" action="/recipes/">\'."\\r\\n";
    //Now a Basic Search box
    $output .= \'    <h2>Keyword Search</h2>\'."\\r\\n";
    $output .= \'    <input type="text" class="field" name="s" id="s" size="19" placeholder="Search" />\'."\\r\\n";

    //Now need to Show the Advance Search Area
    $output .= \'    <h2>Advanced Filters</h2>\'."\\r\\n";
    //Now need to run the system for each Taxonomies
    foreach ( $taxonomies as $tax ) {
        //Need to get the name of each TAX
        //What Taxonomy we want to check
        $args=array(
          \'name\' => $tax
        );
        $outputtype = \'objects\'; // or objects
        $taxonomieslist=get_taxonomies($args,$outputtype); 
        if  ($taxonomieslist) {
            foreach ($taxonomieslist as $taxonomiesone ) {
              $output .= \'  <h3>\'. $taxonomiesone->labels->name. \'</h3>\'."\\r\\n";
            }
        }

        //now get the terms in the Taxonomies
        $output .= epix_recipes_taxterms($tax);
    }

    //now need to end the form with the correct sytack

    //First a submit button
    $output .= \'    <input type="submit" class="submit" name="submit" id="searchsubmit" value="submit" />\'."\\r\\n";
    //Now the closing for tag
    $output .= \'</form\'."\\r\\n";

    //Need to return the form
    return $output;
   }

  //This will get the taxonomies terms
  function epix_recipes_taxterms($tax){

    //Need to get each term and return it
    $returnoutput = "";
    $terms = get_terms($tax,\'hide_empty=0\');
    if ( !empty( $terms ) && !is_wp_error( $terms ) ){
        //$returnoutput .= "  <select name=".$tax.">"."\\r\\n";
        $i = 0;
        foreach ( $terms as $term ) {
            $returnoutput .= \'      <label><input type="checkbox" name="\'.$tax.\'[]" value="\'.$term->slug.\'">\' . $term->name . \'</label>\'."\\r\\n";
            //$i++;[\'.$i.\']
        }
        //$returnoutput .= "  </select>"."\\r\\n";
    }
    return $returnoutput;
   }
如果有人能帮我找出为什么会发生这种情况,或者找到一种阻止WordPress发布这些错误的方法,那将非常有帮助。

是的,我打开了WordPress调试!

2 个回复
SO网友:Diogo Gomes

这里有一个选择<请给出更具体的名称,例如。custom_search_form “提交”按钮:

(...)
//First a submit button
$output .= \'<input type="submit" class="submit" name="custom_search_form" id="searchsubmit" value="submit" />\'."\\r\\n";
然后可以将其添加到函数中。php文件:

add_action( \'parse_request\', \'my_custom_search_form\' );
function my_custom_search_form($query) {
  //return if it not our custom search form
  if ( ! isset( $query->query_vars[ \'custom_search_form\' ] ) )
    return $query;

  //handle form here
}
此外,在注册分类法时,不要忘记将query\\u var设置为true:

\'query_var\'         => true,

SO网友:mrwweb

一些注释。它们可能无法解决您的所有问题,但可能会有所帮助。

在里面epix_recipes_search(), 您正在使用get_taxonomies() 好像是get_taxonomy() 在您的foreach

您可以替换所有这些:

$args=array(
    \'name\' => $tax
);
$outputtype = \'objects\'; // or objects
$taxonomieslist=get_taxonomies($args,$outputtype);
使用此选项:

$taxonomieslist = get_taxonomy( $tax )
The$taxonomieslist 有点用词不当,因为它只包含一个分类法对象(我可能会相应地重命名它$tax_object). 因此,您也可以放弃所有这些:

if ($taxonomieslist) {
    foreach ($taxonomieslist as $taxonomiesone ) {
        $output .= \'  <h3>\'. $taxonomiesone->labels->name. \'</h3>\'."\\r\\n";
    }
}
并将其替换为:

$output .= \'  <h3>\'. $taxonomieslist->labels->name. \'</h3>\'."\\r\\n";
您的</form> 标签

结束