Remove category & tag base from WordPress url - wi

2019-01-02 23:33发布

I would like to remove the category & tag base from wordpress URL. I have come across other posts and solutions which used plugins. I would like to stay away from plugins and have a solution from within functions.php. This would prevent any future plugin updates or wordpress default files from being changed.

Any help would be appreciated. Thanks!

I have tried these solutions so far:

13条回答
forever°为你锁心
2楼-- · 2019-01-03 00:04
add_action( 'init', 'remove_category_perma' );
function remove_category_perma() {
    unset($GLOBALS['wp_rewrite']->extra_permastructs['category']);
}
查看更多
可以哭但决不认输i
3楼-- · 2019-01-03 00:05

If you use Yoast SEO plugin just go to:

Advanced > Permalinks > Change URLs

And select remove from Strip the category base (usually /category/) from the category URL.

Regarding the tag removal I did not found any solution yet.

查看更多
Viruses.
4楼-- · 2019-01-03 00:07

Adding "." or "/" won't work if you want a consolidated blog view. Also, I have know idea what that solutions would do for the RSS or XML feeds. I feel better sticking with the WP convention. However, I did come up with a more elegant approach.

First, I name the base category url "blog"

Then I created a category called "all". Finally, I but all my subcategories under "all". So I get a url structure like this.

/blog - 404 - recommend 301 redirect to /blog/all/    
/blog/all/ -  all posts combined.
/blog/all/category1/ - posts filtered by category1
/blog/all/category2/ - posts filterer by category2

I put a custom label on the menu item called "Blog", but it goes to blog/all. It would be a good idea to 301 redirect /blog to /blog/all in the .htaccess file to avoid the 404 on /blog.

查看更多
放我归山
5楼-- · 2019-01-03 00:09

updated answer:

other solution:
In wp-includes/rewrite.php file, you'll see the code:
$this->category_structure = $this->front . 'category/'; just copy whole function, put in your functions.php and hook it. just change the above line with:
$this->category_structure = $this->front . '/';

查看更多
放荡不羁爱自由
6楼-- · 2019-01-03 00:13
  1. Set Custom Structure: /%postname%/
  2. Set Category base: . (dot not /)

  3. Save. 100% work correctly.

查看更多
霸刀☆藐视天下
7楼-- · 2019-01-03 00:13

Whilst you dismiss it as a solution, the plugin is by far the easiest and most consistent method and they don't change any wordpress default files.

http://wordpress.org/plugins/wp-no-category-base/

It hasn't needed to be updated for a year, so it is not exactly creating any problems with updates.

There is no simple hand rolled solution that will do all of this that does not just replicate what the plugin does from within your own functions.php

  • Better and logical permalinks like myblog.com/my-category/ and myblog.com/my-category/my-post/.
  • Simple plugin - barely adds any overhead.
  • Works out of the box - no setup needed. No need to modify wordpress files.
  • Doesn't require other plugins to work.
  • Compatible with sitemap plugins.
  • Works with multiple sub-categories.
  • Works with WordPress Multisite.
  • Redirects old category permalinks to the new ones (301 redirect, good for SEO).

Plus you get the benefit that if wordpress does change, then the plugin will be updated to work whilst you would then have to figure out how to fix your own code on your own.

查看更多
登录 后发表回答