去掉WordPress分类目录链接中的/category/的方法!

在使用WordPress博客程序建站的时候,有些站长朋友会发现一个问题,就是网站的分类目录链接地址中,会多出一个/category/的地址符号,其实因为WordPress是一个动态的链接地址,这里面出现的/category/地址链接符号并没有任何的意义,反倒是给人一种冗余复杂毫无意义的鸡肋感,对于一些有强迫症的小伙伴来说,十有八九是要去掉它的,互联网的方法很多:

例如在 WordPress 后台点击「设置」菜单下的「固定链接」子菜单,将「分类目录前缀」设置为 . 就可以:

或者使用相关的插件:

No Category Base (WPML)

大家可以去WordPress的后台插件市场搜索一下,然后进行安装就可以,非常简单好用的插件。或者使用我这款插件代码:

<?php
/*
Plugin Name: Category URL Remove
Plugin URI: https://www.hellosys.cn/
Description: This plugin removes the category from the URL.
Version: 1.0
Author: Your Name
Author URI: https://www.hellosys.cn/
*/

// Add settings page to WordPress admin menu
function category_url_remove_menu() {
    add_options_page( 'Category URL Remove', 'Category URL Remove', 'manage_options', 'category-url-remove', 'category_url_remove_options' );
}
add_action( 'admin_menu', 'category_url_remove_menu' );

// Create settings page
function category_url_remove_options() {
    if ( !current_user_can( 'manage_options' ) ) {
        wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
    }
    ?>
    <div class="wrap">
        <h2>Category URL Remove Settings</h2>
        <form method="post" action="options.php">
            <?php settings_fields( 'category-url-remove-settings' ); ?>
            <?php do_settings_sections( 'category-url-remove-settings' ); ?>
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">Remove Category from URL</th>
                    <td>
                        <fieldset>
                            <legend class="screen-reader-text"><span>Remove Category from URL</span></legend>
                            <label for="category-url-remove"><input name="category-url-remove" type="radio" id="category-url-remove" value="1" <?php checked( get_option( 'category-url-remove' ), 1 ); ?>>是</label>
                            <br>
                            <label for="category-url-keep"><input name="category-url-remove" type="radio" id="category-url-keep" value="0" <?php checked( get_option( 'category-url-remove' ), 0 ); ?>>否</label>
                        </fieldset>
                    </td>
                </tr>
            </table>
            <?php submit_button(); ?>
        </form>
    </div>
    <?php
}

// Register settings
function category_url_remove_register_settings() {
    register_setting( 'category-url-remove-settings', 'category-url-remove' );
}
add_action( 'admin_init', 'category_url_remove_register_settings' );

// Remove category from URL
function category_url_remove_remove_category( $link ) {
    if ( get_option( 'category-url-remove' ) == 1 ) {
        $link = str_replace( '/article/category/', '/', $link );
    }
    return $link;
}
add_filter( 'category_link', 'category_url_remove_remove_category' );
add_filter( 'post_link', 'category_url_remove_remove_category' );
add_filter( 'page_link', 'category_url_remove_remove_category' );

为了方便大家使用,上传一份附件,下载之后可以放到wordpress的插件目录之中进行设置。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值