url rewriting - Rewrite rule can't get the ID from rewrited

I use this simple rule :add_rewrite_rule('^product([0-9]+)-([^]+)?$','index.php?pagename=product&id

I use this simple rule :

add_rewrite_rule(
    '^product/([0-9]+)-([^/]+)?$',
    'index.php?pagename=product&id=$matches[1]&slug=$matches[2]',
    'top'
);

It works, it redirects to the correct page I have created, but I can't get the id param, I tried in the template of my page :

global $wp_query;

// undefined index
var_dump($wp_query->query_vars['id']);

I want my URL to be like this :


Dunno if it's possible ?

I use this simple rule :

add_rewrite_rule(
    '^product/([0-9]+)-([^/]+)?$',
    'index.php?pagename=product&id=$matches[1]&slug=$matches[2]',
    'top'
);

It works, it redirects to the correct page I have created, but I can't get the id param, I tried in the template of my page :

global $wp_query;

// undefined index
var_dump($wp_query->query_vars['id']);

I want my URL to be like this :

http://example/product/324-example-of-slug

Dunno if it's possible ?

Share Improve this question asked Jul 16, 2019 at 8:29 Vincent DecauxVincent Decaux 2252 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Ok, you need to add the id param to the list of public query variables :

add_filter('query_vars', 'query_vars');
function query_vars($public_query_vars)
{
    $public_query_vars[] = 'id';
    return $public_query_vars;
}

Thanks to this plugin for the help : https://wordpress/plugins/monkeyman-rewrite-analyzer/#reviews

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745318622a4622336.html

相关推荐

  • url rewriting - Rewrite rule can't get the ID from rewrited

    I use this simple rule :add_rewrite_rule('^product([0-9]+)-([^]+)?$','index.php?pagename=product&id

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信