admin管理员组文章数量:1328051
I am working on a plugin that creates lists. After creating a list, I wanted to remove the slug from the url
Post type:
$rewrite = [
'slug' => 'single-link',
'with_front' => false,
'pages' => false,
'feeds' => false,
];
$args = [
'label' => esc_html__( 'Single Link', 'single-link' ),
'labels' => $labels,
'supports' => [ 'title' ],
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 15,
'menu_icon' => 'dashicons-admin-links',
'show_in_admin_bar' => false,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
'show_in_rest' => true,
];
$args = apply_filters( 'single-link/post_type/args', $args );
register_post_type( 'single-link', $args );
Remove the slug from the URL:
function remove_cpt_slug( $post_link, $post, $leavename ) {
if ( 'single-link' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'remove_cpt_slug', 10, 3 );
function change_slug_struct( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'single-link', 'page' ) );
}
}
add_action( 'pre_get_posts', 'change_slug_struct' );
(this code is from here)
Now after hitting publish, the slug /single-link/ gets deleted, but we always get a 404 when visiting the page. Changing/re-Saving the permalinks did not help. What am I doing wrong?
I am working on a plugin that creates lists. After creating a list, I wanted to remove the slug from the url
Post type:
$rewrite = [
'slug' => 'single-link',
'with_front' => false,
'pages' => false,
'feeds' => false,
];
$args = [
'label' => esc_html__( 'Single Link', 'single-link' ),
'labels' => $labels,
'supports' => [ 'title' ],
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 15,
'menu_icon' => 'dashicons-admin-links',
'show_in_admin_bar' => false,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
'show_in_rest' => true,
];
$args = apply_filters( 'single-link/post_type/args', $args );
register_post_type( 'single-link', $args );
Remove the slug from the URL:
function remove_cpt_slug( $post_link, $post, $leavename ) {
if ( 'single-link' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'remove_cpt_slug', 10, 3 );
function change_slug_struct( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'single-link', 'page' ) );
}
}
add_action( 'pre_get_posts', 'change_slug_struct' );
(this code is from here)
Now after hitting publish, the slug /single-link/ gets deleted, but we always get a 404 when visiting the page. Changing/re-Saving the permalinks did not help. What am I doing wrong?
Share Improve this question asked Jan 21, 2018 at 18:28 ParanoiaParanoia 252 silver badges11 bronze badges3 Answers
Reset to default 3 +100The registering of the custom post type and the permalink modification is OK. The problem is with the WordPress rewrite rules that more than likely will match the "cleaned up" URL of your simple links to pages and it will set the pagename
query var not name
as your change_slug_struct()
function assumed.
So change the function to this to account for all cases:
function change_slug_struct( $query ) {
if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'single-link', 'page' ) );
} elseif ( ! empty( $query->query['pagename'] ) && false === strpos( $query->query['pagename'], '/' ) ) {
$query->set( 'post_type', array( 'post', 'single-link', 'page' ) );
// We also need to set the name query var since redirect_guess_404_permalink() relies on it.
$query->set( 'name', $query->query['pagename'] );
}
}
add_action( 'pre_get_posts', 'change_slug_struct' );
You have to alter the perma structure. By default your custom post type's post will only be found wenether the url starts with the slug prefix. When changing the prefix you will have similar issues as when deleting it, have a look at this post.
To achieve removal of the post type slug prefix you should hook into single-link_rewrite_rules
and iterate through those rules and remove the prefix there as well.
Note: changes in the perma structure may cause url conflicts.
For multiple Custom Post Types adjust like this
$query->set( 'post_type', array( 'post', 'custom1', 'page' ) && array( 'post', 'custom2', 'page' ) );
本文标签: plugin developmentRemove Slug from Custom Post Type results in 404
版权声明:本文标题:plugin development - Remove Slug from Custom Post Type results in 404 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742240926a2438827.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论