在主题中,通过如下代码,就能调出搜索表单了:
<?php get_search_form( $echo ); ?>
-------------
Display search form using searchform.php Theme file.
https://codex.WordPress.org/Function_Reference/get_search_form
如果主题里没有 searchform.php 的话,会使用 WordPress 内置的函数 get_search_form 。
如果要建一个 searchform.php 的话,代码如下:
<form action="/" method="get">
<fieldset>
<label for="search">Search in <?php echo home_url( '/' ); ?></label>
<input type="text" name="s" id="search" value="<?php the_search_query(); ?>" />
<input type="image" alt="Search" src="<?php bloginfo( 'template_url' ); ?>/images/search.png" />
</fieldset>
</form>
使用 get,并 action 到首页。
-------------
当然,你不用模板文件,在 function.php 写下面代码也一样
function my_search_form( $form ) { $form = '<form role="search" method="get" id="searchform" class="searchform" action="' . home_url( '/' ) . '" > <div><label class="screen-reader-text" for="s">' . __( 'Search for:' ) . '</label> <input type="text" value="' . get_search_query() . '" name="s" id="s" /> <input type="submit" id="searchsubmit" value="'. esc_attr__( 'Search' ) .'" /> </div> </form>'; return $form; } add_filter( 'get_search_form', 'my_search_form' );
---------------
按分类搜索:
<input type="text" value="22" name="cat" id="scat" /> value="22" 表示分类的 id
<input type="text" value="movie" name="catgory_name" id="scat" /> value="movie" 表示是 slug
如果是 select:
<select name="cat">
<?php
$categories = get_categories();
foreach ($categories as $category) {
echo '<option value="', $category->term_id, '">', $category->name, "</option>n";
}
?>
</select>
-------
<select name="category_name">
<?php
$categories = get_categories();
foreach ($categories as $category) {
echo '<option value="', $category->slug, '">', $category->name, "</option>n";
}
?>
</select>
参考: http://www.ashuwp.com/courses/supplement/649.html
-------------