Php 自定义帖子类型URL段塞中缺少连字符/破折号(-)
我用下面的代码在WordPress中创建了一个新的自定义帖子类型(收入成绩单)。我的自定义帖子类型的名称是“收入成绩单”,共2个单词。因此,slug应该是“收入成绩单”。相反,URL是“earningstranscripts”。如果我错了或者我遗漏了什么,请纠正我Php 自定义帖子类型URL段塞中缺少连字符/破折号(-),php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,我用下面的代码在WordPress中创建了一个新的自定义帖子类型(收入成绩单)。我的自定义帖子类型的名称是“收入成绩单”,共2个单词。因此,slug应该是“收入成绩单”。相反,URL是“earningstranscripts”。如果我错了或者我遗漏了什么,请纠正我 function custom_post_type() { // Earnings Transcripts $labels = array( 'name' =>
function custom_post_type() {
// Earnings Transcripts
$labels = array(
'name' => _x( 'Earnings Transcripts', 'Post Type General Name', 'twentysixteen' ),
'singular_name' => _x( 'Earnings Transcripts', 'Post Type Singular Name', 'twentysixteen' ),
'menu_name' => __( 'Earnings Transcripts', 'twentysixteen' ),
'parent_item_colon' => __( 'Parent Earnings Transcripts', 'twentysixteen' ),
'all_items' => __( 'All Earnings Transcripts', 'twentysixteen' ),
'view_item' => __( 'View Earnings Transcripts', 'twentysixteen' ),
'add_new_item' => __( 'Add New Earnings Transcripts', 'twentysixteen' ),
'add_new' => __( 'Add New', 'twentysixteen' ),
'edit_item' => __( 'Edit Earnings Transcripts', 'twentysixteen' ),
'update_item' => __( 'Update Earnings Transcripts', 'twentysixteen' ),
'search_items' => __( 'Search Earnings Transcripts', 'twentysixteen' ),
'not_found' => __( 'Not Found', 'twentysixteen' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentysixteen' ),
);
$args = array(
'label' => __( 'Earnings Transcripts', 'twentysixteen' ),
'description' => __( 'Earnings Transcripts', 'twentysixteen' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
'taxonomies' => array( 'Earnings Transcripts' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
// Registering your Custom Post Type
register_post_type( 'Earnings Transcripts', $args );
}
add_action( 'init', 'custom_post_type', 0 );
此外,在查看此自定义帖子类型下的详细帖子时,我不希望此slug出现在帖子URL之前的URL中
例如,在没有自定义post类型slug的情况下,此自定义post类型下的详细post的URL应如下所示
www.domainname.com/post-url/
"rewrite" => array( "slug" => "earnings-transcripts", "with_front" => true ),
在$args
中添加此参数,它将起作用。您必须更改
// Registering your Custom Post Type
register_post_type( 'Earnings Transcripts', $args );
这样的变化:
register_post_type('earnings-transcripts', $args);
以下功能将从permalink中移除段塞
function rf_remove_slug( $post_link, $post, $leavename ) {
if ( 'earnings-transcripts' != $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', 'rf_remove_slug', 10, 3 );
下面的函数将修复在该自定义帖子类型下的详细帖子页面上显示404 Not found错误的问题
function rf_parse_request( $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', 'earnings-transcripts', 'page' ) );
}
}
add_action( 'pre_get_posts', 'rf_parse_request' );
谢谢,您的代码在各个方面都运行良好。在自定义帖子类型的登录页上,它工作正常,但我想在查看自定义帖子类型下的详细帖子时删除“收入成绩单”slug。你能具体说明我应该找哪一个答案吗?你只需要点击链接,它就会自动滚动并带你到我正在谈论的答案。谢谢你的代码在各个方面都很好地工作。在自定义帖子类型的登录页上,其工作正常,但我想在查看该自定义帖子类型下的详细帖子时删除“收入成绩单”slug。检查此WP插件和代码:slug已删除,但在详细帖子上显示未找到页面。我已更新我的答案。请在functions.php文件中添加上述两个函数,我在上面的回答中提到了这一点。