WordPress函数get_search_form()
发布:WordPress教程网 发布时间:
2023-01-13 游览次数:33次
get_search_form( bool $echo = true )
显示搜索框。
介绍
默认会调用 searchform.php 文件,如果主题中没有这个文件,会使用 wp-includes/general-template.php 中的 get_search_form 函数。
使用
get_search_form( $echo );
默认会自动输出。
示例
主题搜索框
在你的主题目录中新建一个php文件 searchform.php,插入:
<form action="/" method="get">
<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%20bloginfo(%20'template_url'%20);%20?>/images/search.png" />
</form>
之后就可以在主题的文件中,使用
<?php get_search_form();?>
来调用搜索功能。
显示一个HTML5标签的搜索框
WordPress 3.6开始,只是html 5标签了,以下代码加入 functions.php 文件中。
/**
* Add HTML5 theme support.
*/
function wpdocs_after_setup_theme() {
add_theme_support( 'html5', array( 'search-form' ) );
}
add_action( 'after_setup_theme', 'wpdocs_after_setup_theme' );
构建搜索函数:
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
<label>
<span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span>
<input type="search" class="search-field"
placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>"
value="<?php echo get_search_query() ?>" name="s"
title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" />
</label>
<input type="submit" class="search-submit"
value="<?php echo esc_attr_x( 'Search', 'submit button' ) ?>" />
</form>
这里看到了吗,input 支持了 type="search" 属性了。
构建一个HTML 4的搜索框
可以将一下代码放入你的主题模板中。
<form role="search" method="get" id="searchform"
class="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<div>
<label class="screen-reader-text" for="s"><?php _x( 'Search for:', 'label' ); ?></label>
<input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit"
value="<?php echo esc_attr_x( 'Search', 'submit button' ); ?>" />
</div>
</form>
标签 : wordpress函数 , 搜索
本文版权归原作者所有,转载请注明原文来源出处, WordPress教程网 感谢您的支持!
本文链接: http://www.wpcn.net/714.html
推荐内容
热点阅读
相关内容
- WordPress手动升级详细步骤
- WordPress手动安全升级
- WordPress后台无插件显示文章和分类ID
- WordPress优化:wp_head和remove_action函数
- query_posts函数把你的wordpress博客变成CMS
- Bootstrap替换WordPress的get_search_form()…
- WordPress 函数:register_sidebar()创建主…
- wp_list_comments()使用回调函数自定义评论…
- WordPress过滤器(Filters):apply_filters和…
- WordPress函数:comments_template(加载评…
- WordPress函数:comment_form( )个性化评论…
- 无处不在的WordPress的主循环
- WordPress 函数do_action()详解和应用举例
- WordPress函数:add_menu_page()后台添加顶…
- WordPress函数:add_theme_page()后台添加设…
- WordPress函数:add_submenu_page()后台为顶…
- WordPress 函数:get_template_part()调用你…
- WordPress函数:load_theme_textdomain()(…
- WordPress 3D旋转彩色标签云
- WordPress文本小工具运行PHP
- WordPress无插件实现主题彩色标签云的N种方…
- WordPress函数:wp_tag_cloud(标签云)详解和…
- WordPress函数:register post type (自定义…
- WordPress使用register_post_type 函数创建…