duancao2082 2016-09-27 09:55
浏览 104

Wordpress:如何仅为特定页面创建自定义搜索表单

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.

  • 写回答

1条回答 默认 最新

  • dsxsou8465 2016-09-27 10:03
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向