我想从WordPress URL中删除类别和标记库。我遇到过使用插件的其他帖子和解决方案。我想远离插件并从functions.php中获得解决方案。这将阻止任何未来的插件更新或WordPress默认文件被更改。
任何帮助,将不胜感激。谢谢!
到目前为止我尝试过这些解决方案:
我喜欢这个解决方案:
如果要从网址中删除/category/
,请执行以下两个步骤:
/%category%/%postname%/
.
保存它,您将看到您的URL更改为以下格式:http:/yourblog.com/quotes/
(来源:http://premium.wpmudev.org/blog/daily-tip-quick-trick-to-remove-category-from-wordpress-url/)
在永久链接中选择自定义结构,并在您的域后添加/%category%/%postname%/。将“/”添加到类别库不起作用,您必须添加句点/点。我在这里写了一个教程:remove category from URL tutorial
其他方案:
在wp-includes / rewrite.php文件中,您将看到代码:
$this->category_structure = $this->front . 'category/';
只是复制整个函数,放入你的functions.php并挂钩它。只需更改以上行:
$this->category_structure = $this->front . '/';
add_action( 'init', 'remove_category_perma' );
function remove_category_perma() {
unset($GLOBALS['wp_rewrite']->extra_permastructs['category']);
}
WordPress 5.0.2:
要从现有帖子中删除类别slug,请执行以下操作:
/%category%/%postname%/
更改为:/%postname%/
所有帖子现在都可以通过domain.com/%postname%/
直接访问,所有类别都可以通过domain.com/category/xyz/
访问。 WordPress会自动为旧网址添加所有301重定向。因此,如果有人访问domain.com/%category%/%postname%/
,他们将自动重定向到domain.com/%postname%/
。
添加“。”如果您想要整合的博客视图,则“/”将无效。此外,我知道该解决方案将为RSS或XML提要做些什么。我觉得最好坚持WP大会。但是,我确实提出了一种更优雅的方法。
首先,我将基类别命名为url“blog”
然后我创建了一个名为“all”的类别。最后,我,但我所有的子类别都在“所有”之下。所以我得到了这样的网址结构。
/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 / all。在.htaccess文件中301重定向/博客到/ blog / all是个好主意,以避免在/ blog上使用404。
虽然您将其视为一种解决方案,但该插件是迄今为止最简单,最一致的方法,并且它们不会更改任何WordPress默认文件。
http://wordpress.org/plugins/wp-no-category-base/
它不需要更新一年,因此它不会完全导致任何更新问题。
没有简单的手动解决方案可以完成所有这些,而不仅仅是在您自己的函数中复制插件的功能.php
另外,如果WordPress确实发生了变化,那么你将获得这样的好处,然后插件将更新为工作,然后你必须弄清楚如何自己修复自己的代码。
如果您使用Yoast SEO
插件,请转到:
Advanced > Permalinks > Change URLs
并从remove
中选择Strip the category base (usually /category/) from the category URL
。
关于标签删除我还没有找到任何解决方案。
而把它放在你的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 );
非类别插件对我不起作用。
对于Multisite WordPress,以下工作:
\
下开放网站;/%category%/%postname%/
。这将显示您的网址为www.domainname.com/categoryname/postname
;yourdoamainname/blog/
。忽略它。如果您现在保存,您在步骤4中所做的工作将被覆盖。此步骤打开固定链接页面但不保存更新数据库所需。如果您仍在搜索组合(网址上的标签,类别和页面),您可以像我一样进行搜索。
.
)设置一个点(https://premium.wpmudev.org/blog/removing-category-base-urls-wordpress/)使用Wordpress 3.9.1测试
如果您有相同名称的页面,类别或标签,系统将采用:
点法可能会破坏你的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;
}
https://wordpress.org/plugins/remove-category-url/使用这个插件它可以完美地隐藏类别库它不需要任何设置只需安装和激活。