wordpress添加媒体
The majority of WordPress search forms set a single querystring parameter named ‘s’:
大多数WordPress搜索表单都设置了一个名为's'的单个querystring参数:
<form method="get" action="<?php bloginfo('url'); ?>"> <fieldset> <input type="text" name="s" value="" placeholder="search…" maxlength="50" required="required" /> <button type="submit">Search</button> </fieldset> </form>There’s nothing wrong with that and I thought it was the only option for many, many years.
这没什么错,我认为这是很多年来唯一的选择。
However, more advanced queries are possible without the aid of plugins (although plenty are available). Deep within the WordPress core, the application parses several other querystring parameters and uses them to return a more focused set of search results. There is some rudimentary documentation which lists the parameter names:
但是,无需插件即可进行更高级的查询(尽管有很多可用的 )。 在WordPress核心的深处,该应用程序解析其他几个querystring参数,并使用它们返回更集中的搜索结果集。 有一些基本文档列出了参数名称:
attachment 附件 attachment_id attachment_id author 作者 author_name author_name cat 猫 category_name 分类名称 comments_popup comments_popup day 天 error 错误 feed 饲料 hour 小时 m 米 minute 分钟 monthnum 月数 name 名称 p p page_id page_id paged 分页 pagename 页面名称 post_parent 后父母 post_type post_type preview 预习 second 第二 static 静态的 subpost 子帖子 subpost_id subpost_id tag 标签 tag_id tag_id tb 待定 w w year 年I’m not convinced all these work as expected and some are a little pointless, but they match the parameters you can pass to WP_Query. We can therefore create an advanced search form using HTML with a smidgen of PHP to automate the options.
我不相信所有这些工作都可以按预期进行,有些工作没有意义,但是它们与您可以传递给WP_Query的参数匹配。 因此,我们可以使用带有smidgenHTMLHTML创建高级搜索表单,以自动执行选项。
You can limit results to a category by passing its slug to the category_name parameter, e.g.
您可以通过将子句传递给category_name参数来将结果限制为一个类别,例如
http://yoursite.com/?s=search+term&category_name=kittensOur search form can allow the user to refine their search to specific categories:
我们的搜索表可以使用户将搜索范围缩小到特定类别:
<form method="get" action="<?php bloginfo('url'); ?>"> <fieldset> <input type="text" name="s" value="" placeholder="search…" maxlength="50" required="required" /> <select name="category_name"> <option value="">all categories</option> <option value="kittens">cute kittens</option> <option value="puppies">adorable puppies</option> </select> <button type="submit">Search</button> </fieldset> </form>If you’d rather list all categories, add the following code between the <select> and </select> tags:
如果要列出所有类别,请在<select>和</select>标记之间添加以下代码:
<?php // generate list of categories $categories = get_categories(); foreach ($categories as $category) { echo '<option value="', $category->slug, '">', $category->name, "</option>\n"; } ?>Search results can be limited to a tag by passing its slug to the tag parameter, e.g.
通过将搜索结果的参数传递给tag参数,可以将搜索结果限制为tag
http://yoursite.com/?s=search+term&tag=cockroachYour search from could therefore limit results to certain tags, e.g.
因此,您从中进行搜索可能会将结果限制为某些标签,例如
<form method="get" action="<?php bloginfo('url'); ?>"> <fieldset> <input type="text" name="s" value="" placeholder="search…" maxlength="50" required="required" /> <select name="tag"> <option value="">any tag</option> <option value="cockroach">cockroaches</option> <option value="snake">snakes</option> </select> <button type="submit">Search</button> </fieldset> </form>Similarly, you can generate a list of all tags for the select field:
同样,您可以为select字段生成所有标签的列表:
<?php // generate list of tags $tags = get_tags(); foreach ($tags as $tag) { echo '<option value="', $tag->slug, '">', $tag->name, "</option>\n"; } ?>What if you want to refine the search by multiple values? For example, the user could choose two or more tags and resulting pages must have them all set. We cannot achieve this using URL parameters alone but let’s start by defining an HTML search form:
如果要通过多个值优化搜索,该怎么办? 例如,用户可以选择两个或多个标签,并且结果页面必须全部设置好它们。 我们不能仅使用URL参数来实现此目的,而是让我们从定义HTML搜索表单开始:
<form method="get" action="<?php bloginfo('url'); ?>"> <fieldset> <input type="text" name="s" value="" placeholder="search…" maxlength="50" required="required" /> <p>Refine search to posts containing chosen tags:</p> <?php // generate list of tags $tags = get_tags(); foreach ($tags as $tag) { echo '<label>', '<input type="checkbox" name="taglist[]" value="', $tag->slug, '" /> ', $tag->name, "</label>\n"; } ?> <button type="submit">Search</button> </fieldset> </form>Note that I’ve used a PHP array parameter named taglist. You can use any name other than those already reserved by WordPress (see the list above).
请注意,我使用了一个名为taglistPHP数组参数。 您可以使用WordPress已经保留的名称以外的任何其他名称(请参见上面的列表)。
We can now intercept a search submission in our WordPress theme’s functions.php file. The advanced_search_query function detects whether a search is active then sets the WP_Query tag_slug__and parameter accordingly.
现在,我们可以在WordPress主题的functions.php文件中拦截搜索提交。 advanced_search_query函数检测搜索是否处于活动状态,然后相应地设置WP_Query tag_slug__and参数。
// advanced search functionality function advanced_search_query($query) { if($query->is_search()) { // tag search if (isset($_GET['taglist']) && is_array($_GET['taglist'])) { $query->set('tag_slug__and', $_GET['taglist']); } return $query; } }Finally, we use the pre_get_posts action hook to run our advanced_search_query function before a query is executed:
最后,在执行查询之前,我们使用pre_get_posts操作挂钩运行我们的advanced_search_query函数:
add_action('pre_get_posts', 'advanced_search_query', 1000);Adding advanced search facilities to WordPress is remarkably easy yet few developers realize it’s possible … perhaps because documentation and examples are a little sparse. I discovered it by accident so I hope you find this code useful in your next WordPress project.
在WordPress中添加高级搜索功能非常容易,但很少有开发人员意识到这是可能的…… 也许是因为文档和示例很少 。 我偶然发现了它,因此希望您在下一个WordPress项目中发现此代码有用。
翻译自: https://www.sitepoint.com/add-advanced-search-wordpress-site/
wordpress添加媒体