在使用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的插件目录之中进行设置。