城市简码
Recently one of our regular users ask if there was an easy way to add the WordPress search form in your post or page content using a shortcode. Well, the answer to that question is Yes. In this article, we will show you how to add the WordPress search form in your post or page content by creating a WordPress search shortcode.
最近,我们的一位普通用户问是否有一种简单的方法可以使用简码在您的帖子或页面内容中添加WordPress搜索表单。 好吧,这个问题的答案是肯定的。 在本文中,我们将向您展示如何通过创建WordPress搜索简码在您的帖子或页面内容中添加WordPress搜索表单。
All you have to do is open your theme’s functions.php file or a site-specific plugin and paste the following code:
您所要做的就是打开主题的functions.php文件或特定于站点的插件,然后粘贴以下代码:
add_shortcode('wpbsearch', 'get_search_form');
Then use the shortcode in your post/page content like so: [wpbsearch]
然后在您的帖子/页面内容中使用简码,如下所示: [wpbsearch]
This will display the default search form. If you want to display a custom search form, then you can do so like this:
这将显示默认的搜索表单。 如果要显示自定义搜索表单,则可以这样进行:
function wpbsearchform( $form ) {
$form = '<form role="search" method="get" id="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_shortcode('wpbsearch', 'wpbsearchform');
We hope that article will help all those who wanted to create a search form shortcode in WordPress.
我们希望本文对所有希望在WordPress中创建搜索表单简码的人有所帮助。
城市简码