最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Search only working on front page (index) , not working on other pages

programmeradmin3浏览0评论

I'm currently building a custom WordPress template and I've run into a problem with the search box.

When I use the searchbox from the homepage, it works as intended. It also works as long as Permalinks are set to default. (url/?page_id=1)

However, when I use the searchbox from another page when using pretty permalinks, the URL looks something like this: /?s=query and a 404 page appears.

My search box code looks like this

<form id="search-query"> <div class="search-box"> <form role="search" method="get" id="searchform" class="searchform" action="/index.php"> <div> <label class="screen-reader-text" for="s"><?php _x( 'Search for:', 'label' ); ?></label> <input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" /> <input type="submit" id="searchsubmit" value="<?php echo esc_attr_x( 'Search', 'submit button' ); ?>" /> </div> </form> </div> </form>

Here's the site's URL if anyone's want to take a look: /

Any help is very appreciated!

I'm currently building a custom WordPress template and I've run into a problem with the search box.

When I use the searchbox from the homepage, it works as intended. It also works as long as Permalinks are set to default. (url/?page_id=1)

However, when I use the searchbox from another page when using pretty permalinks, the URL looks something like this: http://url/pretty-page-title/?s=query and a 404 page appears.

My search box code looks like this

<form id="search-query"> <div class="search-box"> <form role="search" method="get" id="searchform" class="searchform" action="/index.php"> <div> <label class="screen-reader-text" for="s"><?php _x( 'Search for:', 'label' ); ?></label> <input type="text" value="<?php echo get_search_query(); ?>" name="s" id="s" /> <input type="submit" id="searchsubmit" value="<?php echo esc_attr_x( 'Search', 'submit button' ); ?>" /> </div> </form> </div> </form>

Here's the site's URL if anyone's want to take a look: http://hermodsgymnasiet-develop.kaigan.modxcloud/

Any help is very appreciated!

Share Improve this question asked Nov 25, 2015 at 15:36 ErikaErika 1071 silver badge6 bronze badges 1
  • Try http://url/pretty-page-title/?s=query with a bundled theme-- one of the "Twenty {Something}" ones. – s_ha_dum Commented Nov 25, 2015 at 15:55
Add a comment  | 

2 Answers 2

Reset to default 0

Currently your form looks like:

<form id="search-query">
    <p class="in-english"><a href="http://localhost/hermodswp/?page_id=76">Information in English</a></p>
    <div class="search-box">                
        <div>
            <label class="screen-reader-text" for="s"></label>
            <input type="text" value="sdf" name="s" id="s" style="width: 293px;">
            <input type="submit" id="searchsubmit" value="Sök">
        </div>
    </div>
</form>

Try adjusting your submit action to point to your home_url().

action="<?php echo home_url(); ?>"

You can edit the page in Chrome to see that this works fine.

<form role="search" id="search-query" method="get" action="http://hermodsgymnasiet-develop.kaigan.modxcloud/">
    <p class="in-english"><a href="http://localhost/hermodswp/?page_id=76">Information in English</a></p>
    <div class="search-box">        
        <div>
            <label class="screen-reader-text" for="s"></label>
            <input type="text" value="" name="s" id="s" style="width: 293px;">
            <input type="submit" id="searchsubmit" value="Sök">
        </div>
    </div>
</form>

Simply add action="/"to your form!

发布评论

评论列表(0)

  1. 暂无评论