0

I want to create my own url like this http://localhost/wptest/content/programs/trainings-conferences/

I have create my own plugin and use this code.

add_action('init','wpyog_add_rewrite_rules');
add_action('init','wpyog_add_rewrite_rules');

function wpyog_add_rewrite_rules(){
    add_rewrite_rule('^content/([^/]*)/([^/]*)/?','index.php?content_category=$matches[1]content_slug=$matches[2]','top');
}

add_action('query_vars','wpyog_add_query_vars');
function wpyog_add_query_vars( $qvars ) {
    $qvars[] = 'content_category';
    $qvars[] = 'content_slug';
    return $qvars;
}

add_action('template_redirect','wpyog_template_redirect');
function wpyog_template_redirect(){
   if( get_query_var('content_slug') && get_query_var('content_category') ) 
  {
    include( WPYog_Ukraine_PATH.'templates/frontend_template.php' );
    exit();
    }
 }

But its not working its shows 404 page. While I am using this url its works http://localhost/wptest/?category=programs&category_slug=trainings-conferences

3
  • 1
    Missing & in rewrite pattern. Commented Jun 11, 2019 at 12:53
  • @birgire Can you please correct it in proper way Commented Jun 11, 2019 at 12:56
  • also did you resave/flush the permalinks after changing the rewrite rules? Commented Jun 11, 2019 at 13:08

1 Answer 1

1

your rewrite patern miss a "&" between $matches[1] & content_slug

function wpyog_add_rewrite_rules(){
    add_rewrite_rule('^content/([^/]*)/([^/]*)/?','index.php?content_category=$matches[1]&content_slug=$matches[2]','top');
}
8
  • Can you please help me to generate without content word. Such as localhost/wptest/programs/trainings-conferences . Is it will effect page and subpages url Commented Jun 12, 2019 at 6:20
  • just take off content/ from the rule Commented Jun 12, 2019 at 8:31
  • I have create add_rewrite_rule('^/([^/]*)/([^/]*)/([^/]*)/?','index.php?news_detail=$matches[1]news_category=$matches[2]news_slug=$matches[3]','top'); Want something like localhost/wptest/news/economic-development/testing-news-content Commented Jun 13, 2019 at 10:08
  • and, does-it works ? Commented Jun 13, 2019 at 10:35
  • No this not work. But also not showing 404 error Commented Jun 13, 2019 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.