有时你需要写新的动作。要解决类别库问题,需要添加和删除一些操作。您可以将此代码作为自定义插件进行尝试。也许这可以解决你的问题。代码完全删除/category/
基础
将此代码添加到主题functions.php
或者使用此代码创建自定义插件。
/**
* Add some actions to flush rewrite rules
*/
add_action(\'created_category\', \'flush_rewrite_rules_f_custom\');
add_action(\'delete_category\', \'flush_rewrite_rules_f_custom\');
add_action(\'edited_category\', \'flush_rewrite_rules_f_custom\');
// Init function
add_action(\'init\', \'remove_url_perma_cat\');
function flush_rewrite_rules_f_custom()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function remove_url_perma_cat()
{
global $wp_rewrite;
$wp_rewrite->extra_permastructs[\'category\'][0] = \'%category%\';
}
/**
* Add some filter for redirect
*/
add_filter(\'category_rewrite_rules\', \'remo_rew_rul_c\');
add_filter(\'query_vars\', \'remo_q_vars_c\');
add_filter(\'request\', \'remo_crw_req\');
/**
* Remove catergory base and redirect
*/
function remo_rew_rul_c($category_rewrite)
{
global $wp_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 (0 != $category->parent) {
$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]\';
}
$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?crw=$matches[1]\';
return $category_rewrite;
}
/**
* New Query Vars : crw
*/
function remo_q_vars_c($public_query_vars)
{
$public_query_vars[] = \'crw\';
return $public_query_vars;
}
/**
* Redirect based crw
*/
function remo_crw_req($query_vars)
{
if (isset($query_vars[\'crw\'])) {
$catlink = trailingslashit(get_option(\'home\')) . user_trailingslashit($query_vars[\'crw\'], \'category\');
status_header(301);
header("Location: $catlink");
exit;
}
}