簡體   English   中英

如何在永久鏈接設置為“/%postname%/”的情況下從 URL 中刪除類別庫

[英]How to remove category base from URL with permalink set to “/%postname%/”

目前,我有兩個類別來分隔我的 WordPress 博客文章。

  • site.com/category/slow/
  • site.com/category/fast/

如您所見,我網站的類別庫是“類別”。

我在“設置”->“永久鏈接”下的儀表板上的永久鏈接設置設置為“site.com/%postname%/”。

這意味着我的所有博客帖子都將顯示為:“site.com/post-title/”,無論我將帖子分配到哪個類別。

問題是,我想從 URL 中刪除類別庫,以便它們變為:

  • site.com/slow/
  • site.com/fast/

同時,在不更改永久鏈接設置的情況下,我的博客帖子的 URL 繼續看起來像“site.com/post-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');

重寫你的,htaccess 文件

RewriteRule ^/([a-zA-Z0-9-/]+)$ 類別/文件名.php?value=$1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM