首页 文章

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

提问于
浏览
37

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

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

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

13 回答

  • 15

    我喜欢这个解决方案:

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

    • 转到设置>>固定链接并选择自定义并输入: /%category%/%postname%/

    • 接下来将您的类别基地设置为 .

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

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

  • -3
    • 设置自定义结构:/%postname%/

    • 设置类别基数: . (点不是/)

    • 保存 . 100%正常工作 .

  • 0

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

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

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

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

    • 更好,更合理的永久链接,如myblog.com/my-category/和myblog.com/my-category/my-post/ .

    • 简单的插件 - 几乎不增加任何开销 .

    • 开箱即用 - 无需安装 . 无需修改wordpress文件 .

    • 不需要其他插件工作 .

    • 与sitemap插件兼容 .

    • 适用于多个子类别 .

    • 适用于WordPress Multisite .

    • 将旧类别永久链接重定向到新类别(301重定向,适用于SEO) .

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

  • -1

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

    Advanced > Permalinks > Change URLs
    

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

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

  • 2

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

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

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

    对于Multisite WordPress,以下工作:

    • 转到网络管理站点;

    • \ 下的开放网站;

    • 转到设置;

    • 在永久链接结构类型 /%category%/%postname%/ 下 . 这会将您的网址显示为 www.domainname.com/categoryname/postname ;

    • 现在转到您的站点仪表板(不是网络仪表板);

    • 打开设置;

    • 开启固定链接 . 不要保存(永久链接会将不可编辑的字段显示为 yourdoamainname/blog/ . 忽略它 . 如果您现在保存,您在步骤4中所做的工作将被覆盖 . 此步骤打开固定链接页面但不保存更新数据库所需 .

  • 10

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

    使用Wordpress 3.9.1测试

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

    • 标签

    • 类别

  • 0

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

  • 0

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

  • 4
    add_action( 'init', 'remove_category_perma' );
    function remove_category_perma() {
        unset($GLOBALS['wp_rewrite']->extra_permastructs['category']);
    }
    
  • 17

    更新答案:

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

  • 2

    添加“ . ”如果您想要整合的博客视图,则“/”将无效 . 此外,我知道该解决方案将为RSS或XML提要做些什么 . 我觉得最好坚持WP大会 . 但是,我确实提出了一种更优雅的方法 .

    首先,我命名基地类别网址“博客”

    然后我创建了一个名为“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是个好主意,以避免404 / blog .

相关问题