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

时间:2018-12-14 10:56:02

标签: wordpress

请帮助。

有一个自定义帖子类型“ 电影”和一个分类法“ 类型”,具有以下永久链接结构:

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

2 个答案:

答案 0 :(得分:0)

添加此行

flush_rewrite_rules( false );

之后

register_post_type('films', $args);

如果无法解决此问题,请先转到永久链接设置,然后保存为自定义并运行,然后将其更改为后置类型。

希望这可以解决问题。

答案 1 :(得分: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 ); 

在复制和粘贴文件的整个过程中效果很好