从WordPressurl中删除类别和标签的基础 – 没有插件

我想从wordpress URL中删除类别和标签的基础。 我遇到过使用插件的其他post和解决scheme。 我想远离插件,并从functions.php中解决。 这将阻止任何未来的插件更新或WordPress的默认文件被更改。

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

到目前为止我已经尝试了这些解

  • 此Htaccess解决scheme无法正常工作: http : //mikepayne.co/2011/remove-category-base-from-url/
  • 这些方法也失败了: http : //www.askapache.com/wordpress/remove-category-wordpress-urls.html

我喜欢这个解决scheme:

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

  1. 转到设置>>固定链接并select自定义并input: /%category%/%postname%/
  2. 接下来将您的类别库设置为.

保存它,你会看到你的url更改为这种格式:http:/yourblog.com/quotes/

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

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

  3. 保存。 100%正常工作。

虽然你解雇它作为一个解决scheme,插件是迄今为止最简单和最一致的方法,他们不改变任何WordPress的默认文件。

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

它不需要更新一年,所以它不是完全创build更新的任何问题。

没有一个简单的手动解决scheme,将做所有这些不只是复制插件在你自己的functions.php

  • 更好和合乎逻辑的固定链接,如myblog.com/my-category/和myblog.com/my-category/my-post/。
  • 简单的插件 – 几乎不会增加开销。
  • 开箱即用 – 无需安装。 无需修改wordpress文件。
  • 不需要其他插件工作。
  • 与网站地图插件兼容。
  • 适用于多个子类别。
  • 适用于WordPress多站点。
  • 将旧类别固定链接redirect到新链接(301redirect,适合search引擎优化)。

再加上你得到的好处是,如果WordPress的确改变了,那么插件将被更新为工作,而你将不得不弄清楚如何自行修复你自己的代码。

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

 Advanced > Permalinks > Change URLs 

然后select从Strip the category base (usually /category/) from the category URL remove Strip the category base (usually /category/) from the category URL

关于标签删除,我还没有find任何解决scheme。

相反,把这个在你的functions.php工作正常,没有redirect的问题。

 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 ); 

非分类插件没有为我工作。

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

  1. 去networkingpipe理网站;
  2. \下打开网站;
  3. 前往设置;
  4. 在永久链接结构types/%category%/%postname%/ 。 这将显示您的url为www.domainname.com/categoryname/postname ;
  5. 现在转到您的网站仪表板(而不是networking仪表板);
  6. 打开设置;
  7. 打开固定链接。 不要保存(永久链接将显示不可编辑的字段作为yourdoamainname/blog/ 。忽略它。如果你现在保存你在第4步做的工作将被覆盖。这一步打开永久链接页面,但不保存需要更新数据库。

如果您仍然在search组合(标签,类别和页面上的网页),你可以像我一样做。

使用Wordpresstesting3.9.1

如果您的网页,类别或标签具有相同的名称,系统将采取:

  1. 标签
  2. 类别

点诀窍可能会毁了你的RSS饲料和/或分页。 但是,这些工作:

 // remove category base 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; } 

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

添加“。” 或“/”将不起作用,如果你想要一个整合的博客视图。 另外,我也知道这个解决scheme会为RSS或XML提要做些什么。 我觉得更好地坚持WP公约。 不过,我确实想出了一个更优雅的方法。

首先,我将基类别url命名为“博客”

然后我创build了一个名为“全部”的类别。 最后,我只是在“全部”下的所有子类别。 所以我得到这样的url结构。

 /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 

我在名为“Blog”的菜单项上放置了一个自定义标签,但是它转到了blog / all。 在.htaccess文件中301redirect/博客到/ blog / all以避免404开启/博客是一个好主意。

更新回答:

其他scheme:
在wp-includes / rewrite.php文件中,你会看到代码:
$this->category_structure = $this->front . 'category/'; 只是复制整个function,把你的functions.php挂钩它。 只需更改上面的行:
$this->category_structure = $this->front . '/';