没有自定义 post 类型 slug 的 WordPress slug
WordPress slug without custom post type slug
我已使用以下代码注册自定义 post 类型:
register_post_type(
array(
'labels' => // array of labels,
'public' => true,
'publicly_queryable'=> true,
'show_ui' => false,
'map_meta_cap' => true,
'show_in_menu' => false,
'query_var' => true,
'rewrite' => array( 'slug' => 'movie' ),
'capability_type' => 'post',
'has_archive' => false,
'exclude_from_search' => true,
'supports' => array('title'),
)
);
问题是,对于这个 post,URL 变得像:
http://yourdomain.com/movie/post_slug/
但是,我需要:
http://yourdomain.com/post_slug/
有什么方法可以从 URL 中删除 post 类型的 slug "movie",例如 post 和页面默认显示在前端?
谢谢
你试过了吗:
'with_front' => bool
Should the permalink structure be prepended with
the front base. (example: if your permalink structure is /blog/, then
your links will be: false->/news/, true->/blog/news/). Defaults to
true
https://codex.wordpress.org/Function_Reference/register_post_type
我认为没有直接的解决方案。如果你非常需要这个,你可以使用 Custom Rewrite For Post Slug
您必须在 init 中注册您的 post 类型电影。
register_post_type('movie', $args);
要替换 post link 中的电影,请使用以下代码。
function remove_my_post_type($post_link, $post, $leavename) {
if ($post->post_type != 'movie' || $post->post_status != 'publish') {
return $post_link;
}
$post_link = str_replace('/' . $post->post_type . '/', '/', $post_link);
return $post_link;
}
add_filter('post_type_link', 'remove_my_post_type', 10, 3);
之后,您可以将 post 类型设置为默认 post 或页面。
function set_my_post($query) {
if(isset($query->query['post_type'])){
return;
}
if (!empty($query->query['name'])) {
$query->set('post_type', array('post', 'movie', 'page'));
}
}
add_action('pre_get_posts', 'set_my_post');
很简单:在 args array
中设置 'rewrite'-> true
。
我已使用以下代码注册自定义 post 类型:
register_post_type(
array(
'labels' => // array of labels,
'public' => true,
'publicly_queryable'=> true,
'show_ui' => false,
'map_meta_cap' => true,
'show_in_menu' => false,
'query_var' => true,
'rewrite' => array( 'slug' => 'movie' ),
'capability_type' => 'post',
'has_archive' => false,
'exclude_from_search' => true,
'supports' => array('title'),
)
);
问题是,对于这个 post,URL 变得像:
http://yourdomain.com/movie/post_slug/
但是,我需要:
http://yourdomain.com/post_slug/
有什么方法可以从 URL 中删除 post 类型的 slug "movie",例如 post 和页面默认显示在前端?
谢谢
你试过了吗:
'with_front' => bool
Should the permalink structure be prepended with the front base. (example: if your permalink structure is /blog/, then your links will be: false->/news/, true->/blog/news/). Defaults to true
https://codex.wordpress.org/Function_Reference/register_post_type
我认为没有直接的解决方案。如果你非常需要这个,你可以使用 Custom Rewrite For Post Slug
您必须在 init 中注册您的 post 类型电影。
register_post_type('movie', $args);
要替换 post link 中的电影,请使用以下代码。
function remove_my_post_type($post_link, $post, $leavename) {
if ($post->post_type != 'movie' || $post->post_status != 'publish') {
return $post_link;
}
$post_link = str_replace('/' . $post->post_type . '/', '/', $post_link);
return $post_link;
}
add_filter('post_type_link', 'remove_my_post_type', 10, 3);
之后,您可以将 post 类型设置为默认 post 或页面。
function set_my_post($query) {
if(isset($query->query['post_type'])){
return;
}
if (!empty($query->query['name'])) {
$query->set('post_type', array('post', 'movie', 'page'));
}
}
add_action('pre_get_posts', 'set_my_post');
很简单:在 args array
中设置 'rewrite'-> true
。