Wordpress:如何为特定页面创建自定义搜索表单 [英] Wordpress: How to create a custom search form only to a specific page

查看:41
本文介绍了Wordpress:如何为特定页面创建自定义搜索表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建只应出现在特定页面上的自定义搜索表单?

  1. 使用这个<?php get_search_form();?> 将 wordpress 的默认搜索表单插入页面.

  2. 添加这个

<块引用>

searchform.php

到我的主题文件夹覆盖整个网站的搜索功能

我想添加一个搜索表单,该表单仅显示来自特定类型的自定义帖子类型的帖子

表单应该只出现在特定页面.(所以其他页面的搜索表单不应该受此影响)

请问如何实现.非常感谢任何帮助.

解决方案

不要修改 searchform.php,因为您想对其他页面使用默认搜索.下面的 html 代码将为您创建一个自定义表单.只需将代码粘贴到模板中,即可获得自定义搜索表单.

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

假设您的自定义帖子类型是书籍".默认情况下,您的搜索结果将使用 search.php 文件.因此,我们将检查该值是否为书籍,然后我们将加载用 books-search.php 编写的自定义搜索查询.否则,您可以加载现有代码 search.php.

在您的 search.php 文件中:

if(isset($_GET['search-type'])) {$type = $_GET['搜索类型'];if($type == '书籍') {load_template(TEMPLATEPATH .'/books-search.php');}}别的{//您当前的代码}

现在在 books-search.php 中使用:

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

这将仅搜索图书帖子类型.

How do you create a custom search form that should appear only on a particular page?

  1. Using this <?php get_search_form(); ?> inserts wordpress's default search form to the page.

  2. Adding this

searchform.php

to my theme folder overwrites search functionality of the whole site

I want to add a search form which display posts from only a specific type of custom post type

and

the form should appear only in a specific page.(So the search forms in other pages should not be affected by this)

How to achieve this please. Any help is much appreciated.

解决方案

Do not modify searchform.php, as you want to use the default search for other pages. The below html code will create a custom form for you. Just paste the code in the template and you got your custom search form.

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

Assuming that your custom post type is 'books'. By default your search results will use search.php file. So, we will check if the value is books then we will load a custom search query written in books-search.php. Otherwise you can load the existing code present is search.php.

In your search.php file:

if(isset($_GET['search-type'])) {
    $type = $_GET['search-type'];
    if($type == 'books') {
        load_template(TEMPLATEPATH . '/books-search.php');
    }
}else{
 // Your current code
}

Now in books-search.php use:

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

This will search only for books post type.

这篇关于Wordpress:如何为特定页面创建自定义搜索表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆