【问题标题】:Wordpress custom url rewriteWordpress 自定义 url 重写
【发布时间】:2016-04-15 16:41:21
【问题描述】:

在wordpress中是否可以重写下面的url。

http://www.example.com/aboutus/?city=newyorkhttp://www.example.com/aboutus/newyork

然后读出 get_query_var('city') ,我如何在 wordpress 中完成这个?

【问题讨论】:

  • 您是否在设置 > 固定链接中正确设置了您的固定链接
  • 感谢您的回答,您能否再解释一下。根据我的说法,上面的场景很难在 wordpress 中实现。我可以做 city/newyork 但我想从 url 中删除 city 并且仍然能够读取 get_query_car('city') ,我想要实现的是为美国的每个城市创建不同的内容,例如
  • 您是否已经看过rewriting API of wordpress (codex),尤其是add_rewrite_tag()add_rewrite_rule() 函数。

标签: wordpress url-rewriting


【解决方案1】:

这是解决方案:

add_filter( 'query_vars', 'add_query_vars_filter' );

function add_query_vars_filter( $vars ){
$vars[] = "city";
return $vars;
}

//https://thereforei.am/2011/10/28/advanced-taxonomy-queries-with-pretty-urls/
function eg_add_rewrite_rules() {
global $wp_rewrite;

$new_rules = array(
        'aboutus/(.+?)/?$' => 'index.php?page_id=274&city=' . $wp_rewrite->preg_index(1)
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'eg_add_rewrite_rules' );


echo get_query_var('city');

【讨论】:

    猜你喜欢
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多