如何从URL中删除永久地址设置为"%postname%"的类别基础。

问题描述 投票:0回答:1

目前,我有两个分类来分隔我的WordPress博客文章。

  • site.comcategoryslow
  • 网站分类快速

如你所见,我的网站的类别基础是 "类别"。

我在 "设置 "下的 "仪表板 "上的永久链接设置为 "site.com%postname%"。

这意味着我所有的博客文章都会这样显示。"site.compost -title",不管我把文章分配到哪个类别。

问题是,我想把URL中的类别基数去掉,这样它们就变成了。

  • site.comslow
  • 网站建设

同时,在不改变永久链接设置的情况下,我的博客文章的URL仍然是 "site.compost-title"。怎样才能实现这些呢?

我修改了一下我找到的代码后,就有了这样的解决方案。

// Remove category base
function no_category_base_permastruct() {
global $wp_rewrite;
$wp_rewrite -> extra_permastructs['category']['struct'] = '%category%';
}
add_action('init', 'no_category_base_permastruct');

// Add our custom category 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 . ')/page/?([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_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');

// Add 'category_redirect' query variable
function no_category_base_query_vars($public_query_vars) {
$public_query_vars[] = 'category_redirect';
return $public_query_vars;
}
add_filter('query_vars', 'no_category_base_query_vars');

// Redirect if 'category_redirect' is set
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;
}
add_filter('request', 'no_category_base_request');
php wordpress categories permalinks
1个回答
0
投票

重写你的,htaccess文件

RewriteRule ^([a-zA-Z0-9-]+)$ categoryfile_name.php?value=$1。

© www.soinside.com 2019 - 2024. All rights reserved.