从 WordPress url 中删除类别和标签库 - 无需插件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17798815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:17:26  来源:igfitidea点击:

Remove category & tag base from WordPress url - without a plugin

wordpresscategoriesbase

提问by cosmoonot

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.

我想从 WordPress URL 中删除类别和标签库。我遇到过其他使用插件的帖子和解决方案。我想远离插件并从functions.php中找到解决方案。这将防止任何未来的插件更新或 WordPress 默认文件被更改。

Any help would be appreciated. Thanks!

任何帮助,将不胜感激。谢谢!

I have tried these solutions so far:

到目前为止,我已经尝试过这些解决方案:

回答by optimiertes

If you want to remove /category/from the url, follow these two steps:

如果/category/要从 url 中删除,请按照以下两个步骤操作:

  1. Go to Settings >> Permalinks and select Custom and enter: /%category%/%postname%/
  2. Next set your Category Base to .
  1. 转到设置>>固定链接并选择自定义并输入: /%category%/%postname%/
  2. 接下来将您的类别基础设置为 .

Save it and you'll see your URL changed to this format: http://yourblog.com/quotes/

保存它,您会看到您的 URL 更改为以下格式:http: //yourblog.com/quotes/

(Source: http://premium.wpmudev.org/blog/daily-tip-quick-trick-to-remove-category-from-wordpress-url/)

(来源:http: //premium.wpmudev.org/blog/daily-tip-quick-trick-to-remove-category-from-wordpress-url/

回答by paulalexandru

If you use Yoast SEOplugin just go to:

如果您使用Yoast SEO插件,请转到:

Search Appearance > Taxonomies > Category URLs.

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

并选择removeStrip the category base (usually /category/) from the category URL

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

关于标签删除,我还没有找到任何解决方案。

回答by Anigel

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.

虽然您认为它是一种解决方案,但该插件是迄今为止最简单、最一致的方法,它们不会更改任何 WordPress 默认文件。

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

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

没有简单的手工解决方案可以完成所有这些,而不仅仅是从您自己的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).
  • 更好和合乎逻辑的永久链接,如 myblog.com/my-category/ 和 myblog.com/my-category/my-post/。
  • 简单的插件 - 几乎不增加任何开销。
  • 开箱即用 - 无需设置。无需修改 WordPress 文件。
  • 不需要其他插件即可工作。
  • 与站点地图插件兼容。
  • 适用于多个子类别。
  • 适用于 WordPress 多站点。
  • 将旧类别永久链接重定向到新类别(301 重定向,有利于 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.

此外,如果 WordPress 确实发生变化,您将获得好处,那么插件将被更新以正常工作,而您则必须弄清楚如何自行修复自己的代码。

回答by user4414801

  1. Set Custom Structure: /%postname%/
  2. Set Category base: . (dot not /)

  3. Save. 100% work correctly.

  1. 设置自定义结构:/%postname%/
  2. 设置类别基础:。(点不是 /)

  3. 节省。100% 正常工作。

回答by Robbert

instead put this in your functions.php works fine, no redirect problems.

而是把它放在你的functions.php 中,工作正常,没有重定向问题。

function fix_slash( $string, $type )
{
global $wp_rewrite;
if ( $wp_rewrite->use_trailing_slashes == false )
{
    if ( $type != 'single' && $type != 'category' )
        return trailingslashit( $string );

    if ( $type == 'single' && ( strpos( $string, '.html/' ) !== false ) )
        return trailingslashit( $string );

    if ( $type == 'category' && ( strpos( $string, 'category' ) !== false ) )
    {
        $aa_g = str_replace( "/category/", "/", $string );
        return trailingslashit( $aa_g );
    }
    if ( $type == 'category' )
        return trailingslashit( $string );
}
return $string;
}

add_filter( 'user_trailingslashit', 'fix_slash', 55, 2 );

回答by Lucian Davidescu

The dot trick will likely ruin your rss feeds and/or pagination. These work, though:

点技巧可能会破坏您的 RSS 提要和/或分页。这些工作,虽然:

add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
function no_category_base_rewrite_rules($category_rewrite) {
    $category_rewrite=array();
    $categories=get_categories(array('hide_empty'=>false));
    foreach($categories as $category) {
        $category_nicename = $category->slug;
        if ( $category->parent == $category->cat_ID )
            $category->parent = 0;
        elseif ($category->parent != 0 )
            $category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
        $category_rewrite['('.$category_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
        $category_rewrite['('.$category_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
        $category_rewrite['('.$category_nicename.')/?$'] = 'index.php?category_name=$matches[1]';
    }
    global $wp_rewrite;
    $old_base = $wp_rewrite->get_category_permastruct();
    $old_base = str_replace( '%category%', '(.+)', $old_base );
    $old_base = trim($old_base, '/');
    $category_rewrite[$old_base.'$'] = 'index.php?category_redirect=$matches[1]';
    return $category_rewrite;
}

// remove tag base
add_filter('tag_rewrite_rules', 'no_tag_base_rewrite_rules');
function no_tag_base_rewrite_rules($tag_rewrite) {
    $tag_rewrite=array();
    $tags=get_tags(array('hide_empty'=>false));
    foreach($tags as $tag) {
        $tag_nicename = $tag->slug;
        if ( $tag->parent == $tag->tag_ID )
            $tag->parent = 0;
        elseif ($tag->parent != 0 )
            $tag_nicename = get_tag_parents( $tag->parent, false, '/', true ) . $tag_nicename;
        $tag_rewrite['('.$tag_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?tag=$matches[1]&feed=$matches[2]';
        $tag_rewrite['('.$tag_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?tag=$matches[1]&paged=$matches[2]';
        $tag_rewrite['('.$tag_nicename.')/?$'] = 'index.php?tag=$matches[1]';
    }
    global $wp_rewrite;
    $old_base = $wp_rewrite->get_tag_permastruct();
    $old_base = str_replace( '%tag%', '(.+)', $old_base );
    $old_base = trim($old_base, '/');
    $tag_rewrite[$old_base.'$'] = 'index.php?tag_redirect=$matches[1]';
    return $tag_rewrite;
}

// remove author base
add_filter('author_rewrite_rules', 'no_author_base_rewrite_rules');
function no_author_base_rewrite_rules($author_rewrite) { 
    global $wpdb;    
    $author_rewrite = array();    
    $authors = $wpdb->get_results("SELECT user_nicename AS nicename from $wpdb->users");    
    foreach($authors as $author) {
        $author_rewrite["({$author->nicename})/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$"] = 'index.php?author_name=$matches[1]&feed=$matches[2]';
        $author_rewrite["({$author->nicename})/page/?([0-9]+)/?$"] = 'index.php?author_name=$matches[1]&paged=$matches[2]';
        $author_rewrite["({$author->nicename})/?$"] = 'index.php?author_name=$matches[1]';
    }      
    return $author_rewrite;}
add_filter('author_link', 'no_author_base', 1000, 2);
function no_author_base($link, $author_id) {
    $link_base = trailingslashit(get_option('home'));
    $link = preg_replace("|^{$link_base}author/|", '', $link);
    return $link_base . $link;
}

回答by Goutham

The non-category plugin did not work for me.

非类别插件对我不起作用。

For Multisite WordPress the following works:

对于多站点 WordPress,以下工作:

  1. Go to network admin sites;
  2. Open site under \;
  3. Go to settings;
  4. Under permalinks structure type /%category%/%postname%/. This will display your url as www.domainname.com/categoryname/postname;
  5. Now go to your site dashboard (not network dashboard);
  6. Open settings;
  7. Open permalink. Do not save (the permalink will show uneditable field as yourdoamainname/blog/. Ignore it. If you save now the work you did in step 4 will be overwritten. This step of opening permalink page but not saving in needed to update the database.
  1. 转到网络管理站点;
  2. 在 下打开站点\
  3. 前往设置;
  4. 在固定链接结构类型下/%category%/%postname%/。这会将您的网址显示为www.domainname.com/categoryname/postname;
  5. 现在转到您的站点仪表板(不是网络仪表板);
  6. 打开设置;
  7. 打开固定链接。不要保存(永久链接将显示不可编辑的字段yourdoamainname/blog/。忽略它。如果现在保存,您在步骤 4 中所做的工作将被覆盖。打开永久链接页面但不保存的这一步需要更新数据库。

回答by SimonSimCity

If you're still searching for the combination (tags, categories and pages on the url-base), you can do it like I did.

如果您仍在搜索组合(url-base 上的标签、类别和页面),您可以像我一样进行。

Tested using Wordpress 3.9.1

使用 Wordpress 3.9.1 测试

If you have pages, categories or tags having the same name, the system will take:

如果您有同名的页面、类别或标签,系统将采用:

  1. tag
  2. page
  3. category
  1. 标签
  2. 类别

回答by Girish

https://wordpress.org/plugins/remove-category-url/Use this plugin it does the job perfectly of hiding the category-base It does not require any setting just install and activate.

https://wordpress.org/plugins/remove-category-url/使用这个插件它可以完美地隐藏类别库它不需要任何设置只需安装和激活。

回答by Jonny Jordan

Select Custom Structure in permalinks and add /%category%/%postname%/ after your domain. Adding "/" to the category base doesn't work, you have to add a period/dot. I wrote a tutorial for this here: remove category from URL tutorial

在永久链接中选择自定义结构并在您的域后添加 /%category%/%postname%/。将“/”添加到类别库不起作用,您必须添加句点/点。我在这里写了一个教程:从 URL 教程中删除类别