我有一个叫“作者”的CPT。在一个小时的搜索之后,我发现为什么这个CPT(由模板“single author.php”管理)有404个访问单页,我意识到有些人确实有用:那些slug中没有连字符的人!
所以对于作家伏尔泰,我的网站。com/作者/伏尔泰/作品。然而,对于“亚历山大大仲马”,我的网站。com/author/alexandre-dumas/给出了一个404。如果我把这个弹头从“亚历山大大仲马”改为“亚历山大大仲马”,不加连字符,它就行了。
我需要在我的作者slug中使用连字符(否则我需要让我的客户手动编辑她创建或创建的每个作者的slug),如何做到这一点?
我的CPT声明:
function my_custom_post_author() {
$labels = array(
\'name\' => _x( \'Book Authors\', \'post type general name\' ),
\'singular_name\' => _x( \'Book Author\', \'post type singular name\' ),
\'add_new\' => _x( \'Add New\', \'book author\' ),
\'add_new_item\' => __( \'Add New Book Author\' ),
\'edit_item\' => __( \'Edit Book Author\' ),
\'new_item\' => __( \'New Book Author\' ),
\'all_items\' => __( \'All Book Author\' ),
\'view_item\' => __( \'View Book Author\' ),
\'search_items\' => __( \'Search Book Author\' ),
\'not_found\' => __( \'No book authors found\' ),
\'not_found_in_trash\' => __( \'No book authors found in the Trash\' ),
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Book Authors\'
);
$args = array(
\'labels\' => $labels,
\'description\' => \'Holds our authors specific data\',
\'public\' => true,
\'menu_position\' => 4,
\'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\', \'custom-fields\'),
\'rewrite\' => array( \'slug\' => \'book-author\',\'with_front\' => FALSE),
\'has_archive\' => true,
);
register_post_type( \'book-author\', $args );
}
add_action( \'init\', \'my_custom_post_author\' );
function my_updated_messages_author( $messages ) {
global $post, $post_ID;
$messages[\'book-author\'] = array(
0 => \'\',
1 => sprintf( __(\'Book Author updated. <a href="%s">View book author</a>\'), esc_url( get_permalink($post_ID) ) ),
2 => __(\'Custom field updated.\'),
3 => __(\'Custom field deleted.\'),
4 => __(\'Book author updated.\'),
5 => isset($_GET[\'revision\']) ? sprintf( __(\'Book author restored to revision from %s\'), wp_post_revision_title( (int) $_GET[\'revision\'], false ) ) : false,
6 => sprintf( __(\'Book author published. <a href="%s">View book author</a>\'), esc_url( get_permalink($post_ID) ) ),
7 => __(\'Book author saved.\'),
8 => sprintf( __(\'Book author submitted. <a target="_blank" href="%s">Preview book author</a>\'), esc_url( add_query_arg( \'preview\', \'true\', get_permalink($post_ID) ) ) ),
9 => sprintf( __(\'Book author scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview book author</a>\'), date_i18n( __( \'M j, Y @ G:i\' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
10 => sprintf( __(\'Book author draft updated. <a target="_blank" href="%s">Preview book author</a>\'), esc_url( add_query_arg( \'preview\', \'true\', get_permalink($post_ID) ) ) ),
);
return $messages;
}
add_filter( \'post_updated_messages\', \'my_updated_messages_author\' );