WordPress去除分類目錄角子老虎機 由來「category」路徑的三種方法

許多使用 WordPress 的朋友希望移除分類連結中預設添加的 /category/ 目錄標識,博弈 老虎機有很多教程可供參考。最簡單的方法是安裝插件,最複雜的是自己寫程式碼。以下將介紹幾種方法供大家參考。

方法一(不建議):

進入 WordPress 後台 -> 設定 -> 固定連結:

將“分類目錄前綴”設為“.”,雖然這種方法設定簡單,電子 老虎機但效果不理想,容易出錯。

方法二(建議):

建議使用一個功能完善的外掛程式(WP No Category Base)來移除 /category/ 目錄標誌。

WP No Category Base 外掛非常簡單易用,安裝後無需任何設定即可生效。

方法三(自訂程式碼,適合喜歡折騰的朋友):

這種方法適合不喜歡使用外掛程式的朋友,吃 角子 老虎機 遊戲只需將以下程式碼複製到當前主題的 functions.php 檔案中:

/*
 Plugin Name: WP No Category Base
 Plugin URI: http://blinger.org/wordpress-plugins/no-category-base/
 Description: 移除分類目錄連結中的 ‘/category/’。
 Version: 1.1.1
 Author: iDope
 Author URI: http://efextra.com/
 */

// 啟用/停用外掛程式或更改分類時刷新規則
register_activation_hook(__FILE__, ‘no_category_base_refresh_rules’);
add_action(‘created_category’, ‘no_category_base_refresh_rules’);
add_action(‘edited_category’, ‘no_category_base_refresh_rules’);
add_action(‘delete_category’, ‘no_category_base_refresh_rules’);
function no_category_base_refresh_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}

register_deactivation_hook(__FILE__, ‘no_category_base_deactivate’);
function no_category_base_deactivate() {
remove_filter(‘category_rewrite_rules’, ‘no_category_base_rewrite_rules’);
// 不再插入自訂規則
no_category_base_refresh_rules();
}

// 移除分類目錄基礎
add_action(‘init’, ‘no_category_base_permastruct’);
function no_category_base_permastruct() {
global $wp_rewrite, $wp_version;
if (version_compare($wp_version, ‘3.4’, ‘<‘)) {
// 3.4 版本之前的支持
$wp_rewrite->extra_permastructs[‘category’][0] = ‘%category%’;
} else {
$wp_rewrite->extra_permastructs[‘category’][‘struct’] = ‘%category%’;
}
}

// 新增自訂分類重寫規則
add_filter(‘category_rewrite_rules’, ‘no_category_base_rewrite_rules’);
function no_category_base_rewrite_rules($category_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 ($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]’;
}
// 支援從舊分類基礎重定向
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]’;
return $category_rewrite;
}

// 新增 ‘category_redirect’ 查詢變量
add_filter(‘query_vars’, ‘no_category_base_query_vars’);
function no_category_base_query_vars($public_query_vars) {
$public_query_vars[] = ‘category_redirect’;
return $public_query_vars;
}

// 如果設定了 ‘category_redirect’,則重定向
add_filter(‘request’, ‘no_category_base_request’);
function no_category_base_request($query_vars) {
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;
}

如果你的網站還很新,可以考慮使用這個方法,角子老虎機 破解因為更短的連結對 SEO 更友善。但如果分類已被收錄或網站已經存在一段時間,最好不要修改。如果必須修改,請確保進行好目錄的 301 重定向。

GIF Overlay
PNG圖片

感謝你進入網站!!邀請你一起遊玩➜財神娛樂城

Floating Ad