从WordPress网址中删除类别和标记库 - 没有插件

问题描述 投票:41回答:14

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

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

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

wordpress categories base
14个回答
51
投票

我喜欢这个解决方案:

如果要从网址中删除/category/,请执行以下两个步骤:

  1. 转到设置>>永久链接并选择自定义并输入:/%category%/%postname%/
  2. 接下来将您的Category Base设置为.

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

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


0
投票

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


0
投票

updated answer:

其他方案: 在wp-includes / rewrite.php文件中,您将看到代码: $this->category_structure = $this->front . 'category/';只是复制整个函数,放入你的functions.php并挂钩它。只需更改以上行: $this->category_structure = $this->front . '/';


0
投票
add_action( 'init', 'remove_category_perma' );
function remove_category_perma() {
    unset($GLOBALS['wp_rewrite']->extra_permastructs['category']);
}

0
投票

WordPress 5.0.2:

要从现有帖子中删除类别slug,请执行以下操作:

  1. 导航到设置>永久链接并将自定义结构从/%category%/%postname%/更改为:/%postname%/
  2. 将类别和标记基础保持为空(这也是默认值)
  3. 保存

所有帖子现在都可以通过domain.com/%postname%/直接访问,所有类别都可以通过domain.com/category/xyz/访问。 WordPress会自动为旧网址添加所有301重定向。因此,如果有人访问domain.com/%category%/%postname%/,他们将自动重定向到domain.com/%postname%/


-3
投票

添加“。”如果您想要整合的博客视图,则“/”将无效。此外,我知道该解决方案将为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。


17
投票
  1. 设置自定义结构:/%postname%/
  2. 设置类别基数:。 (点不是/)
  3. 保存。 100%正常工作。

17
投票

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

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

它不需要更新一年,因此它不会完全导致任何更新问题。

没有简单的手动解决方案可以完成所有这些,而不仅仅是在您自己的函数中复制插件的功能.php

  • 更好,更合理的永久链接,如myblog.com/my-category/和myblog.com/my-category/my-post/。
  • 简单的插件 - 几乎不增加任何开销。
  • 开箱即用 - 无需设置。无需修改WordPress文件。
  • 不需要其他插件工作。
  • 与站点地图插件兼容。
  • 适用于多个子类别。
  • 适用于WordPress Multisite。
  • 将旧类别永久链接重定向到新类别(301重定向,适用于SEO)。

另外,如果WordPress确实发生了变化,那么你将获得这样的好处,然后插件将更新为工作,然后你必须弄清楚如何自己修复自己的代码。


11
投票

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

Advanced > Permalinks > Change URLs

并从remove中选择Strip the category base (usually /category/) from the category URL

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


4
投票

而把它放在你的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 );

2
投票

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

对于Multisite WordPress,以下工作:

  1. 转到网络管理站点;
  2. \下开放网站;
  3. 前往设置;
  4. 根据永久链接结构类型/%category%/%postname%/。这将显示您的网址为www.domainname.com/categoryname/postname;
  5. 现在转到您的站点仪表板(不是网络仪表板);
  6. 打开设置;
  7. 打开永久链接。不保存(固定链接将显示不可编辑的字段为yourdoamainname/blog/。忽略它。如果您现在保存,您在步骤4中所做的工作将被覆盖。此步骤打开固定链接页面但不保存更新数据库所需。

2
投票

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

使用Wordpress 3.9.1测试

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

  1. 标签
  2. 类别

2
投票

点法可能会破坏你的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;
}

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.