自定义帖子类型中的固定链接结构



请提供帮助。

有一个自定义的帖子类型">电影"和一个分类法">流派,具有以下永久链接结构:

http://example.com/films/film-1/
http://example.com/genres/genre-1/

要解决http://example.com/genres/获取404错误

我需要键入链接http://example.com/films/genres/genre-1/上的电影帖子列表http://example.com/films/genres/

提前感谢您的帮助。

我的代码:

function films_catalog() {
	$labels = array(
		'name' => 'Films catalog',
		'singular_name' => 'Film', 
		'add_new' => 'Add film',
		'add_new_item' => 'Add new film',
		'edit_item' => 'Edit film',
		'new_item' => 'New film',
		'all_items' => 'All films',
		'view_item' => 'Show films',
		'search_items' => 'Search films',
		'not_found' =>  'No films.',
		'not_found_in_trash' => 'No films.',
		'menu_name' => 'Films' 
	);
	$args = array(
		'labels' => $labels,
		'public' => true,
		'show_ui' => true,
		'has_archive' => true,
		'menu_icon' => get_stylesheet_directory_uri() .'/img/films_icon.png', 
		'menu_position' => 20, 
		'supports' => array( 'title', 'editor', 'comments', 'author', 'thumbnail'),
	);
	register_post_type('films', $args);
}
add_action( 'init', 'films_catalog' );
function create_films_taxonomies(){
	$labels = array(
		'name' => 'Genres',
		'singular_name' => 'Genres',
		'search_items' =>  'Search genres',
		'popular_items' => 'Popular genres',
		'all_items' => 'All genres',
		'parent_item' => null,
		'parent_item_colon' => null,
		'edit_item' => 'Edit genre',
		'update_item' => 'Update genre',
		'add_new_item' => 'Add new genre',
		'new_item_name' => 'New genre name',
		'separate_items_with_commas' => 'Genres list',
		'add_or_remove_items' => 'Add or remove genres',
		'choose_from_most_used' => 'Popular genres',
		'menu_name' => 'Genres',
	);
	register_taxonomy('genres', array('films'), array(
		'hierarchical' => false,
		'labels' => $labels,
		'show_ui' => true,
		'query_var' => true,
		'rewrite' => array( 'slug' => 'genres' ),
	));
}
add_action( 'init', 'create_films_taxonomies', 0 );	

function films_catalog() {
$labels = array(
'name' => 'Films catalog',
'singular_name' => 'Film', 
'add_new' => 'Add film',
'add_new_item' => 'Add new film',
'edit_item' => 'Edit film',
'new_item' => 'New film',
'all_items' => 'All films',
'view_item' => 'Show films',
'search_items' => 'Search films',
'not_found' =>  'No films.',
'not_found_in_trash' => 'No films.',
'menu_name' => 'Films' 
);
$args = array(
'labels' => $labels,
'public' => true,
'show_ui' => true,
'has_archive' => true,
'menu_icon' => get_stylesheet_directory_uri() .'/img/films_icon.png', 
'menu_position' => 20, 
'supports' => array( 'title', 'editor', 'comments', 'author', 'thumbnail'),
);
register_post_type('films', $args);
flush_rewrite_rules();
}
add_action( 'init', 'films_catalog' );

function create_films_taxonomies(){
$labels = array(
'name' => 'Genres',
'singular_name' => 'Genres',
'search_items' =>  'Search genres',
'popular_items' => 'Popular genres',
'all_items' => 'All genres',
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Edit genre',
'update_item' => 'Update genre',
'add_new_item' => 'Add new genre',
'new_item_name' => 'New genre name',
'separate_items_with_commas' => 'Genres list',
'add_or_remove_items' => 'Add or remove genres',
'choose_from_most_used' => 'Popular genres',
'menu_name' => 'Genres',
);
register_taxonomy('genres', array('films'), array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genres' ),
));
}
add_action( 'init', 'create_films_taxonomies', 0 ); 

这可以很好地复制和粘贴文件

添加此行

flush_rewrite_rules( false );

之后

register_post_type('films', $args);

如果不能解决这个问题,请转到设置permalink首先保存为自定义并运行,然后将其更改为发布类型。

希望这能解决问题。

最新更新