gpt4 book ai didi

php - 我的自定义帖子类型 URL Slug 中缺少连字符

转载 作者:行者123 更新时间:2023-12-04 02:39:05 25 4
gpt4 key购买 nike

我使用以下代码在 WordPress 中创建了一个新的自定义帖子类型( yield 记录)。我的自定义帖子类型的名称是 “Earnings Transcripts”,这是 2 个词。所以 slug 应该是 "earnings-transcripts"。相反,URL 是 “earningstranscripts”。我在这里缺少什么?

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

最佳答案

你必须改变

// Registering your Custom Post Type
register_post_type( 'Earnings Transcripts', $args );

这样的变化:

register_post_type('earnings-transcripts', $args);

下面的函数将从永久链接中删除 slug

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

关于php - 我的自定义帖子类型 URL Slug 中缺少连字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60256523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com