admin管理员组文章数量:1323348
I am learning to build a fully custom WordPress theme. I have a search form
<form role="search" method="get" class="search-form" action="<?php echo home_url() ?>/">
<input type="search" placeholder="Search …" value="" id="s" name="s" required />
<button type="submit"><i class="fa fa-search"></i></button>
</form>
which renders the following HTML:
<form role="search" method="get" class="search-form" action="/">
<input type="search" placeholder="Search …" value="" id="s" name="s" required />
<button type="submit"><i class="fa fa-search"></i></button>
</form>
In my search.php
template, I currently have the following code to check whether everything works as desired:
if( empty( $_GET['s'] ) ) {
echo 'No search';
exit;
}
$s = $_GET['s'];
echo $s;
When I run /?s=sedan
, I get the value of $_GET['s']
printed on the search page perfectly
However, I want the URL to look like the following:
, so I added the following code in my theme's
functions.php
file.
function yd_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'yd_change_search_url' );
After adding the code, I resaved Settings > Permalinks
. But when I run , I get No Search message on the search page.
The phrase No Search is printed when an empty $_GET['s']
is encountered according to the following code that I have added in search.php
template.
if( empty( $_GET['s'] ) ) {
echo 'No search';
exit;
}
$s = $_GET['s'];
echo $s;
What I am doing wrong? I am following this tutorial.
My ultimate goal is to build a URL with multiple query strings, i.e.
/?s=sedan&mfg=2010&model=Mark%20X&cond=used,
which would ultimately transform after rewriting to this
I am learning to build a fully custom WordPress theme. I have a search form
<form role="search" method="get" class="search-form" action="<?php echo home_url() ?>/">
<input type="search" placeholder="Search …" value="" id="s" name="s" required />
<button type="submit"><i class="fa fa-search"></i></button>
</form>
which renders the following HTML:
<form role="search" method="get" class="search-form" action="http://local.devsite/">
<input type="search" placeholder="Search …" value="" id="s" name="s" required />
<button type="submit"><i class="fa fa-search"></i></button>
</form>
In my search.php
template, I currently have the following code to check whether everything works as desired:
if( empty( $_GET['s'] ) ) {
echo 'No search';
exit;
}
$s = $_GET['s'];
echo $s;
When I run http://local.devsite/?s=sedan
, I get the value of $_GET['s']
printed on the search page perfectly
However, I want the URL to look like the following:
http://local.devsite/search/sedan
, so I added the following code in my theme's functions.php
file.
function yd_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'yd_change_search_url' );
After adding the code, I resaved Settings > Permalinks
. But when I run http://local.devsite/search/sedan
, I get No Search message on the search page.
The phrase No Search is printed when an empty $_GET['s']
is encountered according to the following code that I have added in search.php
template.
if( empty( $_GET['s'] ) ) {
echo 'No search';
exit;
}
$s = $_GET['s'];
echo $s;
What I am doing wrong? I am following this tutorial.
My ultimate goal is to build a URL with multiple query strings, i.e.
http://local.devsite/?s=sedan&mfg=2010&model=Mark%20X&cond=used,
which would ultimately transform after rewriting to this
http://local.mydev/sedan/2010/mark-x/used
1 Answer
Reset to default 0I figured it out. The error was in my code in search.php
template file.
My previous code was this:
if( empty( $_GET['s'] ) ) {
echo 'No search';
exit;
}
$s = $_GET['s'];
echo $s;
while it should be this:
if( empty( get_query_var( 's' ) ) ) {
echo 'No search';
exit;
}
$s = get_query_var( 's' );
echo $s;
Since there is no literal query string in the URL anymore and it has been redirected to a new URL with a different pattern where I won't be able to grab the value using $_GET['s']
版权声明:本文标题:Cannot make custom search permalink to work in a fully custom theme. Search string $_GET['s'] is always empty 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742132198a2422213.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论