1

I'm working on a custom static site generator using WordPress. I can't figure out how to remove the slash that comes after the category base and the tag base. I want to replace each with a dash.

I'm currently using a post-processing PHP script and str_replace, but that means I'm stuck with whatever I hard code there (which is "category" and "tag" right now).

Any suggestions?

4
  • Please share have you done so far.
    – Laxmana
    Commented May 5, 2017 at 12:26
  • I haven't done anything. I've "tested" stuff that doesn't work. Commented May 5, 2017 at 12:29
  • You said "I'm currently using a post-processing PHP script and str_replace" so I thought you had something. It's ok share the "tested" stuff
    – Laxmana
    Commented May 5, 2017 at 12:31
  • Well, all that code is in articles on my website and I don't want to post links here. Commented May 5, 2017 at 12:35

1 Answer 1

0

I did a lot of digging and I came up with this code to solve the problem as well as adding ".html" to pages, categories and tags. The permalink setting takes care of posts.

function hpct_page_rewrite($rules) {
  foreach ($rules as $key => $value) {
    $newrules[str_replace('/?', '.html', $key)] = $value;
  }
  return $newrules;
}
function hpct_page_link($link) {
  return $link . '.html';
}
function hpct_category_rewrite($rules) {
  foreach ($rules as $key => $value) {
    $newrules[str_replace('/?', '.html', $key)] = $value;
  }
  return $newrules;
}
function hpct_category_link($link) {
  return str_replace('category/', 'category-', $link) . '.html';
}
function hpct_tag_rewrite($rules) {
  foreach ( $rules as $key => $value ) {
    $newrules[str_replace('/?', '.html', $key)] = $value;
  }
  return $newrules;
}
function hpct_tag_link($link) {
  return str_replace('tag/', 'tag-', $link) . '.html';
}
add_filter('page_rewrite_rules', 'hpct_page_rewrite', 3);
add_filter('page_link', 'hpct_page_link', 1);
add_filter('category_rewrite_rules', 'hpct_category_rewrite', 3);
add_filter('category_link', 'hpct_category_link', 1);
add_filter('tag_rewrite_rules', 'hpct_tag_rewrite', 3);
add_filter('tag_link', 'hpct_tag_link', 1);

Yes, it's hard-coded as well and I'll eventually take care of it. But at least I can do this from within WordPress.

Your Answer

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

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