admin管理员组文章数量:1406944
I want my url to look like this www.example/events/United+States instead of how it currently looks like which is www.example/events/?country=United+States. I'm not quite sure how to go about this. Here is my code
<?php
$some_url = site_url().'/events/';
$params = array( 'country' => urlencode($counter) );
$some_url = add_query_arg( $params, $some_url );
?>
<a class="gpo_country" href="<?php echo $some_url ?>" >
I want my url to look like this www.example/events/United+States instead of how it currently looks like which is www.example/events/?country=United+States. I'm not quite sure how to go about this. Here is my code
<?php
$some_url = site_url().'/events/';
$params = array( 'country' => urlencode($counter) );
$some_url = add_query_arg( $params, $some_url );
?>
<a class="gpo_country" href="<?php echo $some_url ?>" >
Share
Improve this question
asked Oct 24, 2017 at 2:22
NaomiNaomi
32 bronze badges
2
|
1 Answer
Reset to default 0You can create a rewrite rule using add_rewrite_rule()
that will match a given path to query parameters:
function wpse_283774_rewrite() {
add_rewrite_rule( '^events/([^/]+)/?', 'index.php?pagename=events&country=$matches[1]', 'top' );
}
add_action( 'init', 'wpse_283774_rewrite' );
This rule will match whatever is after /events
as the country
query parameter. Setting the third argument to top
means that it will match our rule first, otherwise it will try to match whatever is after /events
as a child page of /events
.
Now we just need to register country
as a valid query parameter:
function wpse_283774_query_vars( $vars ) {
$vars[] = 'country';
return $vars;
}
add_filter( 'query_vars', 'wpse_283774_query_vars' );
Now in your template/functions you can get whatever comes after /events
with get_query_var( 'country' )
:
if ( get_query_var( 'country' ) ) {
echo 'Events for ' . get_query_var( 'country' );
}
Just make sure to re-save your Permalinks settings after adding the code.
本文标签: permalinksrebuilding rewriting a url to make it SEO friendly
版权声明:本文标题:permalinks - rebuilding rewriting a url to make it SEO friendly 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744755643a2623445.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
/events
? Is it a post type archive? A page? – Jacob Peattie Commented Oct 24, 2017 at 3:12