如何覆盖分类法试图强制执行的索引/归档重写?我的分类法gameboy
迫使我以https://wwwmywebsite.com/games
身份拥有的正常页面会自动重定向到https://www.mywebsite.com/games/genre
。我认为,由于分类法包含gameboy
条,因此它会强制覆盖。
我只希望我的常规页面保留/ games /。任何帮助都会很棒。以下是我的一些functions.php文件。
// Register Custom Post Type
function fc_games() {
$labels = array(
'name' => _x( 'Games', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Game', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Games', 'text_domain' ),
'name_admin_bar' => __( 'Game', 'text_domain' ),
'archives' => __( 'Game Archives', 'text_domain' ),
'attributes' => __( 'Game Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Game:', 'text_domain' ),
'all_items' => __( 'All Games', 'text_domain' ),
'add_new_item' => __( 'Add New Game', 'text_domain' ),
'add_new' => __( 'Add New Game Type', 'text_domain' ),
'new_item' => __( 'New Game', 'text_domain' ),
'edit_item' => __( 'Edit Game', 'text_domain' ),
'update_item' => __( 'Update Game', 'text_domain' ),
'view_item' => __( 'View Game', 'text_domain' ),
'view_items' => __( 'View Games', 'text_domain' ),
'search_items' => __( 'Search Games', 'text_domain' ),
'not_found' => __( 'Item Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Item Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into item', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter items list', 'text_domain' ),
);
$args = array(
'label' => __( 'Game', 'text_domain' ),
'description' => __( 'Games', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'revisions', 'custom-fields', 'page-attributes' ),
'taxonomies' => array( 'category', 'post_tag', 'genre_type' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-format-aside',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'games', $args );
}
add_action( 'init', 'fc_games', 0 );
function genre_type() {
$labels = array(
'name' => _x( 'Taxonomy', 'Taxonomy General Name', 'snt' ),
'singular_name' => _x( 'Genre', 'Taxonomy Singular Name', 'snt' ),
'menu_name' => __( 'Genres', 'snt' ),
'all_items' => __( 'All Items', 'snt' ),
'parent_item' => __( 'Parent Item', 'snt' ),
'parent_item_colon' => __( 'Parent Item:', 'snt' ),
'new_item_name' => __( 'New Item Name', 'snt' ),
'add_new_item' => __( 'Add New Item', 'snt' ),
'edit_item' => __( 'Edit Item', 'snt' ),
'update_item' => __( 'Update Item', 'snt' ),
'view_item' => __( 'View Item', 'snt' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'snt' ),
'add_or_remove_items' => __( 'Add or remove items', 'snt' ),
'choose_from_most_used' => __( 'Choose from the most used', 'snt' ),
'popular_items' => __( 'Popular Items', 'snt' ),
'search_items' => __( 'Search Items', 'snt' ),
'not_found' => __( 'Not Found', 'snt' ),
'no_terms' => __( 'No items', 'snt' ),
'items_list' => __( 'Items list', 'snt' ),
'items_list_navigation' => __( 'Items list navigation', 'snt' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'has_archive' => false,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => array(
'slug' => 'games',
'with_front' => false
),
'with_front' => false,
);
register_taxonomy( 'genre_type', array( 'games' ), $args );
}
add_action( 'init', 'genre_type', 0 );
function resources_cpt_generating_rule($wp_rewrite) {
$rules = array();
$terms = get_terms( array(
'taxonomy' => 'genre_type',
'hide_empty' => false,
) );
$post_type = 'games';
foreach ($terms as $term) {
$rules['games/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&resources_post_type=$matches[1]&name=$matches[1]';
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'resources_cpt_generating_rule');
function change_link( $permalink, $post ) {
if( $post->post_type == 'games' ) {
$resource_terms = get_the_terms( $post, 'genre_type' );
$term_slug = '';
if( ! empty( $resource_terms ) ) {
foreach ( $resource_terms as $term ) {
// The featured resource will have another category which is the main one
if( $term->slug == 'featured' ) {
continue;
}
$term_slug = $term->slug;
$term_slug = "genre/";
break;
}
}
if( $post->ID !== 42011 ) {
$permalink = get_home_url() ."/games/" . $term_slug . '' . $post->post_name;
}
}
return $permalink;
}
add_filter('post_type_link',"change_link",10,2);
add_filter( 'register_post_type_args', function( $args, $post_type )
{
// Make sure we only target the games post type
if ( 'games' !== $post_type )
return $args;
$args['has_archive'] = false;
$args['rewrite'] = false;
$args[ 'rewrite' ][ 'with_front' ] = false;
return $args;
}, PHP_INT_MAX, 2);
答案 0 :(得分:0)
slug 中的u表示唯一。我知道这不是令人满意的答案,但是您不应该为分类法和页面注册相同的标签。您为什么不首先用任何类型的“ genre”或“ games_genre”注册分类法?
答案 1 :(得分:0)
如果出于某种原因要覆盖分类标准的子类,则可以轻松地执行此操作,方法是将该功能添加到子主题的functions.php中
function yourprefix_update_taxonomy_slug() {
// get the arguments of the already-registered taxonomy
$args = get_taxonomy( 'genre_type' ); // returns an object
// make changes to the args
// in this example there are three changes
// again, note that it's an object
$args->show_admin_column = true;
$args->rewrite['slug'] = 'games'; /* HERE WE ADDED THE NEW SLUG */
$args->rewrite['with_front'] = false;
// re-register the taxonomy
register_taxonomy( 'genre_type', 'games', (array) $args );
}
// hook it up to 11 so that it overrides the original register_taxonomy function
add_action( 'init', 'yourprefix_update_taxonomy_slug', 11 );
注意:添加此功能后,您需要重置永久链接(否则,访问新链接时会出现404错误)。