drt5813 2016-08-23 13:47
浏览 43

wordpress自定义分类搜索无法正常工作

Hello i am building advanced wordpress custom search with taxonomy filter. But the taxonomy is not being filtered. This is the code for the search form;

<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>" role="search">
    <label for="s" class="assistive-text"><?php _e( 'Search', 'understrap' ); ?></label>
    <div class="input-group">
        <input type="text" class="field form-control" name="s" id="s" placeholder="<?php esc_attr_e( 'Search &hellip;', 'understrap' ); ?>" />
        <input type="hidden" name="post_type" value="country">

        <?php
         $args= array('hide_empty'=>false,
                      );
        $district = get_terms('country',$args); ?>
        <select name="country">
        <option value="" selected="selected"> Select</option>
        <?php foreach ($district as $region) {?>
        <<option value="albania"> <?php echo  $region->name;?></option>
            <?php } ?>  
        </select>


            <input type="submit" class="submit btn btn-primary" name="submit" id="searchsubmit" value="<?php esc_attr_e( 'Search', 'understrap' ); ?>" />
        </span>
    </div>
</form>

This the query from the url:

http://localhost/home/?s=&post_type=country&country=&submit=Search

  • 写回答

1条回答 默认 最新

  • duanchuli5647 2016-08-23 15:13
    关注

    First, make sure your taxonomy is set to public e.g.

    register_taxonomy( 'my_tax', 
            array('for_posttype'), 
            array('hierarchical' => true,     
                'public' => true,
    

    Then verify if you can directly query the post type + taxonomy parameter based on url query var, run directly to browser http://yoursite.com/?post_type=country&taxonomy=your_taxonomy_slug&term=term_slug

    If you don't have any result then you have to do additional debugging like making sure there's already post assign into that term, the term is public etc.

    When you get a result you can then build a custom form to build the url you'd want generate.

    e.g.

    <form method="GET" action="/" >
        <input type="text" name="s" placeholder="keyword" />
        <select name="term">
            <option value="" selected="selected"> Select</option>
            <option value="term1">Term 1</option> 
            <option value="term2">Term 2</option>
        </select>
        <input type="hidden" name="post_type" value="country">
        <input type="hidden" name="taxonomy" value="your_taxonomy_slug">
    </form>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)