WordPress 免插件去除分类链接中「category」字样

0 Comment

如题,WordPress 免插件去除分类链接中「category」字样。有这个需求的同学可以参考一下。代码提取自插件:No Category Base (WPML),不想折腾可以直接用插件。

<?php
/*去除 Category 目录 提取自 No Category Base (WPML) https://wordpress.org/plugins/no-category-base-wpml/*/

/* hooks */
register_activation_hook(__FILE__, 'no_category_base_refresh_rules');
//register_deactivation_hook(__FILE__, 'no_category_base_deactivate');

/* actions */
add_action('created_category', 'no_category_base_refresh_rules');
add_action('delete_category', 'no_category_base_refresh_rules');
add_action('edited_category', 'no_category_base_refresh_rules');
add_action('init', 'no_category_base_permastruct');

/* filters */
add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
add_filter('query_vars', 'no_category_base_query_vars'); // Adds 'category_redirect' query variable
add_filter('request', 'no_category_base_request'); // Redirects if 'category_redirect' is set

function no_category_base_refresh_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}

function no_category_base_deactivate() {
remove_filter( 'category_rewrite_rules', 'no_category_base_rewrite_rules' ); // We don't want to insert our custom rules again
no_category_base_refresh_rules();
}

function no_category_base_permastruct()
{
global $wp_rewrite;
global $wp_version;

if ( $wp_version >= 3.4 ) {
$wp_rewrite->extra_permastructs['category']['struct'] = '%category%';
} else {
$wp_rewrite->extra_permastructs['category'][0] = '%category%';
}
}

function no_category_base_rewrite_rules($category_rewrite) {
global $wp_rewrite;
$category_rewrite=array();

/* WPML is present: temporary disable terms_clauses filter to get all categories for rewrite */
if ( class_exists( 'Sitepress' ) ) {
global $sitepress;

remove_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ) );
$categories = get_categories( array( 'hide_empty' => false ) );
//Fix provided by Albin here https://wordpress.org/support/topic/bug-with-wpml-2/#post-8362218
//add_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ) );
add_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ), 10, 4 );
} else {
$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})/{$wp_rewrite->pagination_base}/?([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
$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;
}

function no_category_base_query_vars($public_query_vars) {
$public_query_vars[] = 'category_redirect';
return $public_query_vars;
}

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;
}

注意:不管安装插件或使用代码均可能出现 404 页面,即设置的固定链接伪静态失效了!

解决方法:登录后台→设置→固定链接设置页面,随意改一下固定链接格式,然后再改回正常使用的固定链接格式即可。如果还会出现 404,可以尝试清除缓存!

快报:Vultr 最新活动,充 $5 送 $25,可以用 $2.5 套餐 VPS 一年,活动即将结束,快上车【点击查看】| 搬瓦工年付 $19 中国直连线路补货,优惠码:BWH1ZBPVK,『点击直达』| 京东优惠券:每天可领
声明: 除非注明,常阳时光文章均为原创,转载请以链接形式标明本文地址
本文地址:https://cyhour.com/775/
Views: 45 Tags:  , 

Comments:0

发表留言

彩蛋:Vultr 充 $5 送 $25,活动即将结束,快上车【点击查看】| 搬瓦工年付 $19 中国直连补货,优惠码:BWH1ZBPVK,『点击直达』| 京东优惠券:每天可领