微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何路由与WordPress的URL中的变量?

如何解决如何路由与WordPress的URL中的变量?

我有一个可通过“ www.myDomain.com/documents”访问的wordpress主题模板页面页面内容是“文档”。

但是我似乎找不到找到将资源添加到URL的方法。 我如何才能在末尾添加“ 123”之类的资源,而仍然将其转到“文档”页面

就像“ www.myDomain.com/documents/123”一样,最后一部分是要进入我的wordpress主题页面代码的ID,现在它刚好是404。

解决方法

add_action( 'init','wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
    add_rewrite_rule(
        'documents/([A-Za-z0-9_-]+)/?$','index.php?pagename=documents&documents_id=$matches[1]','top' );
}

add_filter( 'query_vars','wpse26388_query_vars' );
function wpse26388_query_vars( $query_vars ){
    $query_vars[] = 'documents_id';
    return $query_vars;
}

add_filter('init','flushRules');
function flushRules(){ global $wp_rewrite; $wp_rewrite->flush_rules(); }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。