第二个自定义 post 类型不显示在前端
Second custom post type no showing on frontend
WP 的 post 类型代码正在运行并显示在前端。但是当复制它并将 "collaborations" 替换为 "movies" 时,它在管理员端有效,但在前端为 returns 404。
我删除了 "movie" 代码并尝试了一些在网上找到的示例,同样的问题仍然存在。它适用于 1 post 类型但不超过 1。有什么原因吗?
"collaboration" 代码转到 "www.website.com/collaborations" 这是正确的,但第二个代码转到 "www.website.com/movies/movies/"。我在下面添加了 "movie" 代码。
协作码
function create_collaborations()
{
register_post_type('collaborations',
array('labels' => array('name' => __('collaborations'),'singular_name' => __('Collaborations')),'public' => true,'has_archive' => false,'rewrite' => array('slug' => 'collaborations'))
);
}
add_action('init','create_collaborations');
function cw_post_type_collaborations()
{
$supports = array('title', 'editor', 'thumbnail', 'revisions');
$labels = array(
'name' => _x('Collaborations', 'plural'),
'singular_name' => _x('Collaborations', 'singular'),
'menu_name' => _x('Collaborations', 'admin menu'),
'name_admin_bar'=> _x('Collaborations', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New Collaborations'),
'new_item' => __('New Collaborations'),
'edit_item' => __('Edit Collaborations'),
'view_item' => __('View Collaborations'),
'all_items' => __('View Collaborations'),
'search_items' => __('Search Collaborations'),
'not_found' => __('No Collaborations found.')
);
$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => 'collaborations'),
'has_archive' => true,
'hierarchical' => false
);
register_post_type('collaborations', $args);
}
add_action('init', 'cw_post_type_collaborations');
电影代码
function create_movies()
{
register_post_type('movies',
array('labels' => array('name' => __('movies'),'singular_name' => __('Movies')),'public' => true,'has_archive' => false,'rewrite' => array('slug' => 'movies'))
);
}
add_action('init','create_movies');
function cw_post_type_movies()
{
$supports = array('title', 'editor', 'thumbnail', 'revisions');
$labels = array(
'name' => _x('Movies', 'plural'),
'singular_name' => _x('Movies', 'singular'),
'menu_name' => _x('Movies', 'admin menu'),
'name_admin_bar'=> _x('Movies', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New Movies'),
'new_item' => __('New Movies'),
'edit_item' => __('Edit Movies'),
'view_item' => __('View Movies'),
'all_items' => __('View Movies'),
'search_items' => __('Search Movies'),
'not_found' => __('No Movies found.')
);
$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => 'movies'),
'has_archive' => true,
'hierarchical' => false
);
register_post_type('movies', $args);
}
add_action('init', 'cw_post_type_movies');
为什么相同的 post-type 重复两次?
也许我错了,但我认为你只需要第二个功能。
1.) function cw_post_type_movies()
2.) function cw_post_type_collaborations()
电影只需使用:
function cw_post_type_movies(){
$supports = array('title', 'editor', 'thumbnail', 'revisions');
$labels = array(
'name' => _x('Movies', 'plural'),
'singular_name' => _x('Movies', 'singular'),
'menu_name' => _x('Movies', 'admin menu'),
'name_admin_bar'=> _x('Movies', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New Movies'),
'new_item' => __('New Movies'),
'edit_item' => __('Edit Movies'),
'view_item' => __('View Movies'),
'all_items' => __('View Movies'),
'search_items' => __('Search Movies'),
'not_found' => __('No Movies found.')
);
$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => 'movies'),
'has_archive' => true,
'hierarchical' => false
);
register_post_type('movies', $args);
}
add_action('init', 'cw_post_type_movies');
协作使用:
function cw_post_type_collaborations(){
$supports = array('title', 'editor', 'thumbnail', 'revisions');
$labels = array(
'name' => _x('Collaborations', 'plural'),
'singular_name' => _x('Collaborations', 'singular'),
'menu_name' => _x('Collaborations', 'admin menu'),
'name_admin_bar'=> _x('Collaborations', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New Collaborations'),
'new_item' => __('New Collaborations'),
'edit_item' => __('Edit Collaborations'),
'view_item' => __('View Collaborations'),
'all_items' => __('View Collaborations'),
'search_items' => __('Search Collaborations'),
'not_found' => __('No Collaborations found.')
);
$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => 'collaborations'),
'has_archive' => true,
'hierarchical' => false
);
register_post_type('collaborations', $args);
}
add_action('init', 'cw_post_type_collaborations');
在创建两个自定义-post-types 后转到:
your-domain/wp-admin
设置 > 永久链接 > 保存更改
=> 刷新永久链接
WP 的 post 类型代码正在运行并显示在前端。但是当复制它并将 "collaborations" 替换为 "movies" 时,它在管理员端有效,但在前端为 returns 404。
我删除了 "movie" 代码并尝试了一些在网上找到的示例,同样的问题仍然存在。它适用于 1 post 类型但不超过 1。有什么原因吗?
"collaboration" 代码转到 "www.website.com/collaborations" 这是正确的,但第二个代码转到 "www.website.com/movies/movies/"。我在下面添加了 "movie" 代码。
协作码
function create_collaborations()
{
register_post_type('collaborations',
array('labels' => array('name' => __('collaborations'),'singular_name' => __('Collaborations')),'public' => true,'has_archive' => false,'rewrite' => array('slug' => 'collaborations'))
);
}
add_action('init','create_collaborations');
function cw_post_type_collaborations()
{
$supports = array('title', 'editor', 'thumbnail', 'revisions');
$labels = array(
'name' => _x('Collaborations', 'plural'),
'singular_name' => _x('Collaborations', 'singular'),
'menu_name' => _x('Collaborations', 'admin menu'),
'name_admin_bar'=> _x('Collaborations', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New Collaborations'),
'new_item' => __('New Collaborations'),
'edit_item' => __('Edit Collaborations'),
'view_item' => __('View Collaborations'),
'all_items' => __('View Collaborations'),
'search_items' => __('Search Collaborations'),
'not_found' => __('No Collaborations found.')
);
$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => 'collaborations'),
'has_archive' => true,
'hierarchical' => false
);
register_post_type('collaborations', $args);
}
add_action('init', 'cw_post_type_collaborations');
电影代码
function create_movies()
{
register_post_type('movies',
array('labels' => array('name' => __('movies'),'singular_name' => __('Movies')),'public' => true,'has_archive' => false,'rewrite' => array('slug' => 'movies'))
);
}
add_action('init','create_movies');
function cw_post_type_movies()
{
$supports = array('title', 'editor', 'thumbnail', 'revisions');
$labels = array(
'name' => _x('Movies', 'plural'),
'singular_name' => _x('Movies', 'singular'),
'menu_name' => _x('Movies', 'admin menu'),
'name_admin_bar'=> _x('Movies', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New Movies'),
'new_item' => __('New Movies'),
'edit_item' => __('Edit Movies'),
'view_item' => __('View Movies'),
'all_items' => __('View Movies'),
'search_items' => __('Search Movies'),
'not_found' => __('No Movies found.')
);
$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => 'movies'),
'has_archive' => true,
'hierarchical' => false
);
register_post_type('movies', $args);
}
add_action('init', 'cw_post_type_movies');
为什么相同的 post-type 重复两次?
也许我错了,但我认为你只需要第二个功能。
1.) function cw_post_type_movies()
2.) function cw_post_type_collaborations()
电影只需使用:
function cw_post_type_movies(){
$supports = array('title', 'editor', 'thumbnail', 'revisions');
$labels = array(
'name' => _x('Movies', 'plural'),
'singular_name' => _x('Movies', 'singular'),
'menu_name' => _x('Movies', 'admin menu'),
'name_admin_bar'=> _x('Movies', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New Movies'),
'new_item' => __('New Movies'),
'edit_item' => __('Edit Movies'),
'view_item' => __('View Movies'),
'all_items' => __('View Movies'),
'search_items' => __('Search Movies'),
'not_found' => __('No Movies found.')
);
$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => 'movies'),
'has_archive' => true,
'hierarchical' => false
);
register_post_type('movies', $args);
}
add_action('init', 'cw_post_type_movies');
协作使用:
function cw_post_type_collaborations(){
$supports = array('title', 'editor', 'thumbnail', 'revisions');
$labels = array(
'name' => _x('Collaborations', 'plural'),
'singular_name' => _x('Collaborations', 'singular'),
'menu_name' => _x('Collaborations', 'admin menu'),
'name_admin_bar'=> _x('Collaborations', 'admin bar'),
'add_new' => _x('Add New', 'add new'),
'add_new_item' => __('Add New Collaborations'),
'new_item' => __('New Collaborations'),
'edit_item' => __('Edit Collaborations'),
'view_item' => __('View Collaborations'),
'all_items' => __('View Collaborations'),
'search_items' => __('Search Collaborations'),
'not_found' => __('No Collaborations found.')
);
$args = array(
'supports' => $supports,
'labels' => $labels,
'public' => true,
'query_var' => true,
'rewrite' => array('slug' => 'collaborations'),
'has_archive' => true,
'hierarchical' => false
);
register_post_type('collaborations', $args);
}
add_action('init', 'cw_post_type_collaborations');
在创建两个自定义-post-types 后转到:
your-domain/wp-admin
设置 > 永久链接 > 保存更改
=> 刷新永久链接