0

I created the page template which needs to take some GET variables, currently URL's looks like that:

http://example.com/coupons/?cat_id=25

I need to make that way:

http://example.com/coupons/25

I checked the WP_Rewrite documentation and playing a bit with that stuff, but can't make things work as except.

Regards, Chris

0

First you have to filter query vars and add your query var to the array:

add_filter( 'query_vars', 'wpa56345_query_vars' );

function wpa56345_query_vars( $query_vars ){
    $query_vars[] = 'cat_id';
    return $query_vars;
}

Then your rule which captures any digits after your pagename coupons and passes that as cat_id:

add_action( 'init', 'wpa56345_rewrites' );

function wpa56345_rewrites(){
    add_rewrite_rule(
        'coupons/(\d+)/?$',
        'index.php?pagename=coupons&cat_id=$matches[1]',
        'top'
    );
}

The above both go in your theme's functions.php. Make sure to visit permalinks settings page after adding rewrite rules to flush rules, or they will not take effect. If you put everything into a plugin, you can set it up to flush rules when they are first added.

anyway, in your template, you can then access the value of cat_id with:

echo get_query_var( 'cat_id' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.