0

I need to rewrte this URL http://example.com/news/?news_id=2 to http://example.com/news/2 where news is the page and 2 is the news_id

I have added following code in functions.php:

function add_news_rewrite_rule() {
    add_rewrite_tag("%news_id%", '([^/]*)');
    add_rewrite_rule('^news/([^/]*)/?','index.php?pagename=news&news_id=$matches[1]','top');    
}
add_action( 'init', 'add_news_rewrite_rule' );

function my_insert_query_vars( $vars ){
    array_push($vars, 'news_id');
    return $vars;
}
add_filter( 'query_vars','my_insert_query_vars' ); 

And I am using $wp_query->query_vars['news_id'] instead of $_GET['news_id'] to pull out the data from database.

But this is not working. Please help me.

  • Use get_query_var() instead of directly accessing $wp_query->query_vars['news_id'] – kaiser Mar 18 '13 at 21:23
0

Have a look at the Permalink settings in the admin, you should be able to achieve what you want there. Here is a link to the Permalink Codex

something like /%post_id% should do the job, if you select the 'Custom Structure' option.

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.