urls - Remove Trailing Slash from Category Base and Tag Base

admin2025-01-07  3

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?

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?

Share Improve this question asked May 5, 2017 at 12:20 RT CunninghamRT Cunningham 112 bronze badges 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. – RT Cunningham 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. – RT Cunningham Commented May 5, 2017 at 12:35
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736252795a74.html

最新回复(0)