wordpress表单调用_如何在WordPress中使用多个搜索表单

wordpress表单调用

Recently we showed you how to limit search results for specific post types in WordPress. Now we are going to show you how you can create different/multiple search forms altogether. This way each form can be limited to searching for a specific post type. Although this isn’t very hard it will require you to have a basic understanding of WordPress templates.

最近,我们向您展示了如何限制WordPress中特定帖子类型的搜索结果 。 现在,我们将向您展示如何创建不同的/多个搜索表单。 这样,每种形式都可以限于搜索特定的帖子类型。 尽管这不是很难,但需要您对WordPress模板有基本的了解。

First, you will need some search forms. Place the following code wherever you want them to be in your blog:

首先,您将需要一些搜索表单。 将以下代码放在您希望它们出现在博客中的任何位置:



<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<input type="text" value="" name="s" id="s" />
<input type="hidden" name="search-type" value="normal" />
<input name="submit" type="submit" value="Go" />
</form>


In order to specify what sort of search this form will be doing, just change the value of the hidden field. Right now it is set to “normal” but it can be anything you want. Next, we need to modify the search.php file. Open it up and replace everything in it with this code (copy the existing code to your clipboard first, you will need it in a minute):

为了指定此表单将执行哪种搜索,只需更改隐藏字段的值即可。 现在将其设置为“正常”,但可以是您想要的任何内容。 接下来,我们需要修改search.php文件。 打开它,并用以下代码替换其中的所有内容(首先将现有代码复制到剪贴板中,一分钟之内将需要它):



<?php
if(isset($_GET['search-type'])) {
    $type = $_GET['search-type'];
    if($type == 'random') {
        load_template(TEMPLATEPATH . '/normal-search.php');
    } elseif($type == 'books') {
        load_template(TEMPLATEPATH . '/books-search.php');
    }
}
?>

So we will be assuming that you have two search forms, normal and books. This code is simply redirecting the search to the php file that handles that specific query. Now we just have to create those files. So, go ahead and create a normal-search.php and books-search.php file (just replace “normal” and “books” with whatever values you have been using).

因此,我们假设您有两种搜索形式,普通和书籍。 此代码只是将搜索重定向到处理该特定查询的php文件。 现在我们只需要创建这些文件。 因此,继续创建一个normal-search.php和books-search.php文件(只需将“ normal”和“ books”替换为您一直使用的任何值)即可。

Now, in normal-search.php copy and paste the following code:

现在,在normal-search.php中复制并粘贴以下代码:



$args = array( 'post_type' => 'post' );
$args = array_merge( $args, $wp_query->query );
query_posts( $args );


Immediately after this paste the loop code from your clipboard that you copied from the search.php file. Together, this code will search only your normal blog posts. Now, in the books-search.php file add this bit of code and again paste the loop right after it:

此后,立即粘贴从search.php文件复制的剪贴板中的循环代码。 这些代码一起只能搜索您的普通博客文章。 现在,在books-search.php文件中添加以下代码,然后在其之后再次粘贴循环:



$args = array( 'post_type' => 'books' );
$args = array_merge( $args, $wp_query->query );
query_posts( $args );


This will cause WordPress to search only for the custom post type of “books”. You can repeat this process for as many search forms as you would like.

这将导致WordPress仅搜索“书”的自定义帖子类型。 您可以根据需要为任意多个搜索表单重复此过程。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-use-multiple-search-forms-in-wordpress/

wordpress表单调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值