如何从URL中删除WordPress类别并拥有自定义分页参数/重写?

时间:2016-01-20 作者:envysea

我正在尝试从WordPress URL中删除类别URI,并进行自定义分页重写。我将在下面详细解释发生了什么:

所以我有一个博客。

我们有两个(主要)类别:“酒吧谈话”和“教程”。我将以“酒吧谈话”为例。

我发现,使用WordPress,我实际上可以访问这两个URL,而无需进行任何自定义操作:

- Current: https://scotch.io/category/bar-talk
- Want: https://scotch.io/bar-talk
但是,自动创建的分页会中断:

- Current: https://scotch.io/category/bar-talk/page/2 (works)
- Want: https://scotch.io/bar-talk/page/2 (doesn\'t work)
这很有意义,因为我们还没有进行任何自定义重写。但是,如何获得第二个结构?接下来,我想在“页面”上进行重写,以便我们可以:

- Super want: https://scotch.io/bar-talk/drink-number/2
其他注意事项:

我们之前成功地重写了“https://scotch.io/category/bar-talk/drink-number/2“很好。但我们真的不想让category参数再作为分页的一部分了,任何带有标记的东西都可以正常工作:”https://scotch.io/tag/javascript/drink-number/2

  • 当前永久链接结构为”/%category%/%postname%”
    • 谢谢!

    3 个回复
    SO网友:DrizzlyOwl

    嗯。你有没有想过在中重写一些URL。htaccess? RewriteRule ^category/bar-talk/page/([0-9]+)$ /bar-talk/drink-number/$1 [L,R=301] RewriteRule ^bar-talk/drink-number/([0-9]+)$ /index.php?category_name=bar-talk&page=$1 [L]

    //@毛毛雨

    SO网友:envysea

    好吧,目前正在使用这种疯狂。“页面”重写和修改的组合WP No Category Base 插件。似乎工作得很好。

    /*===========================================================
    =            REMOVE CATEGORY / CUSTOM PAGINATION            =
    ===========================================================*/
    register_activation_hook(__FILE__, \'no_category_base_refresh_rules\');
    add_action(\'created_category\', \'no_category_base_refresh_rules\');
    add_action(\'edited_category\', \'no_category_base_refresh_rules\');
    add_action(\'delete_category\', \'no_category_base_refresh_rules\');
    function no_category_base_refresh_rules() {
        global $wp_rewrite;
        $wp_rewrite -> flush_rules();
    }
    
    register_deactivation_hook(__FILE__, \'no_category_base_deactivate\');
    function no_category_base_deactivate() {
        remove_filter(\'category_rewrite_rules\', \'no_category_base_rewrite_rules\');
        // We don\'t want to insert our custom rules again
        no_category_base_refresh_rules();
    }
    
    // Remove category base
    add_action(\'init\', \'no_category_base_permastruct\');
    function no_category_base_permastruct() {
        global $wp_rewrite, $wp_version;
        if (version_compare($wp_version, \'3.4\', \'<\')) {
            // For pre-3.4 support
            $wp_rewrite -> extra_permastructs[\'category\'][0] = \'%category%\';
        } else {
            $wp_rewrite -> extra_permastructs[\'category\'][\'struct\'] = \'%category%\';
        }
    }
    
    // Add our custom category rewrite rules
    add_filter(\'category_rewrite_rules\', \'no_category_base_rewrite_rules\');
    function no_category_base_rewrite_rules($category_rewrite) {
        //var_dump($category_rewrite); // For Debugging
    
        $category_rewrite = array();
        $categories = get_categories(array(\'hide_empty\' => false));
        foreach ($categories as $category) {
            $category_nicename = $category -> slug;
            if ($category -> parent == $category -> cat_ID)// recursive recursion
                $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 . \')/drink-number/?([0-9]{1,})/?$\'] = \'index.php?category_name=$matches[1]&paged=$matches[2]\';
            $category_rewrite[\'(\' . $category_nicename . \')/?$\'] = \'index.php?category_name=$matches[1]\';
        }
        // Redirect support from Old Category Base
        global $wp_rewrite;
        $old_category_base = get_option(\'category_base\') ? get_option(\'category_base\') : \'category\';
        $old_category_base = trim($old_category_base, \'/\');
        $category_rewrite[$old_category_base . \'/(.*)$\'] = \'index.php?category_redirect=$matches[1]\';
    
        //var_dump($category_rewrite); // For Debugging
        return $category_rewrite;
    }
    
    // Add \'category_redirect\' query variable
    add_filter(\'query_vars\', \'no_category_base_query_vars\');
    function no_category_base_query_vars($public_query_vars) {
        $public_query_vars[] = \'category_redirect\';
        return $public_query_vars;
    }
    
    // Redirect if \'category_redirect\' is set
    add_filter(\'request\', \'no_category_base_request\');
    function no_category_base_request($query_vars) {
        //print_r($query_vars); // For Debugging
        if (isset($query_vars[\'category_redirect\'])) {
            $catlink = trailingslashit(get_option(\'home\')) . user_trailingslashit($query_vars[\'category_redirect\'], \'category\');
            status_header(301);
            header("Location: $catlink");
            exit();
        }
        return $query_vars;
    }
    
    
    register_activation_hook(   __FILE__ , \'pagination_flush\' );
    register_deactivation_hook( __FILE__ , \'pagination_flush\' );
    
    add_action(\'init\', function() {
        $GLOBALS[\'wp_rewrite\']->pagination_base = \'drink-number\';
    });
    
    function pagination_flush() {
        add_action( \'init\', \'flush_rewrite_rules\', 11 );
    }
    

    SO网友:chaithra

    无需编写任何代码,您只需安装、删除CPT基本插件,并在此插件中进行一些设置并激活即可。链接:https://wordpress.org/plugins/remove-cpt-base/

    相关推荐

    change pagination url

    目前,我的分页页面URL为:http://www.example.com/category_name/page/2但我需要将此URL结构更改为:http://www.example.com/category_name/?page=2等有什么解决方案吗?