希望您能解决我遇到的问题。我有一个称为Symposium的自定义帖子类型。我也有2个其他自定义帖子类型,分别称为“扬声器”和“演示文稿”。我已经能够将演示文稿和演讲者的帖子类型分配为专题讨论会帖子类型的子代。但是,我现在正在尝试更改单个演示文稿和单个发言人的永久链接结构。目前,我正处于WordPress后端的永久链接正在更新以反映该目标的时刻:
单个演示 https://example.com/sposium-2019/presentation/how-to-moonwalk
单扬声器 https://example.com/syposium-2019/speaker/cool-person
如果查看单个演示文稿的页面,则会看到正确的内容。但是,查看单个扬声器的页面会导致“找不到页面”错误。我还尝试添加第三个子自定义帖子类型“会话”,更改了永久链接结构,但它还会显示“找不到页面”。
如果我更改子级自定义帖子类型的注册顺序-将演讲者移至演示文稿之前,则演讲者会起作用,但演讲者不会。非常有趣。我已经清除了永久链接,对此我感到很困惑。
在这里,我正在注册自定义帖子类型-父级(专题讨论会)和子级自定义帖子类型(发言人,演示文稿,会话)。
一个部分
// Register Custom Post Types
function my_register_cpt() {
$labels = array(
'name' => _x( 'symposium', 'Post Type General Name', 'gbc' ),
'singular_name'=> _x( 'Symposium', 'Post Type Singular Name', 'gbc' ),
'menu_name' => __( 'Symposiums', 'gbc' ),
'name_admin_bar => __( 'Symposiums', 'gbc' ),
'all_items' => __( 'All Symposiums', 'gbc' ),
'view_item' => __( 'View Symposium', 'gbc' ),
'add_new_item' => __( 'Add New Symposium', 'gbc' ),
'add_new' => __( 'Add New', 'gbc' ),
'edit_item' => __( 'Edit Symposium', 'gbc' ),
'update_item' => __( 'Update Symposium', 'gbc' ),
'search_items' => __( 'Search Symposium', 'gbc' ),
'not_found' => __( 'Not Found', 'gbc' ),
'not_found_in_trash' => __( 'Not found in Trash', 'gbc' ),
);
$args = array(
'label' => __( 'Symposium', '1fix' ),
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'menu_icon' => 'dashicons-carrot',
'menu_position' => 5,
'supports' => array( 'title', 'editor','thumbnail', 'page-attributes' ),
);
register_post_type( 'symposium', $args );
$labels = array(
'name' => _x( 'presentation', 'Post Type General Name', 'gbc' ), 'singular_name' => _x( 'Presentations', 'Post Type Singular Name', 'gbc' ),
'menu_name' => __( 'Presentations', 'gbc' ),
'name_admin_bar' => __( 'Presentations', 'gbc' ),
'all_items' => __( 'All Presentations', 'gbc' ),
'view_item' => __( 'View Presentation', 'gbc' ),
'add_new_item' => __( 'Add New Presentation', 'gbc' ),
'add_new' => __( 'Add New', 'gbc' ),
'edit_item' => __( 'Edit Presentation', 'gbc' ),
'update_item' => __( 'Update Presentation', 'gbc' ),
'search_items' => __( 'Search Presentation', 'gbc' ),
'not_found' => __( 'Not Found', 'gbc' ),
'not_found_in_trash' => __( 'Not found in Trash', 'gbc' ),);
$args = array(
'label' => __( 'Presentations', 'gbc' ),
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'rewrite' => array(
'slug' => 'presentation',
'with_front' => false,
),
'menu_icon' => 'dashicons-money',
'show_in_menu' => 'edit.php?post_type=symposium'
);
register_post_type( 'presentation', $args );
$labels = array(
'name' => _x( 'speaker', 'Post Type General Name', 'gbc' ),
'singular_name' => _x( 'Speaker', 'Post Type Singular Name', 'gbc' ),
'menu_name' => __( 'Speakers', 'gbc' ),
'name_admin_bar' => __( 'Speakers', 'gbc' ),
'all_items' => __( 'All Speakers', 'gbc' ),
'view_item' => __( 'View Speaker', 'gbc' ),
'add_new_item' => __( 'Add New Speaker', 'gbc'),
'add_new' => __( 'Add New', 'gbc' ),
'edit_item' => __( 'Edit Speaker', 'gbc' ),
'update_item' => __( 'Update Speaker', 'gbc' ),
'search_items' => __( 'Search Speaker', 'gbc' ),
'not_found' => __( 'Not Found', 'gbc' ),
'not_found_in_trash' => __( 'Not found in Trash', 'gbc' ),
);
$args = array(
'label' => __( 'Speakers', 'gbc' ),
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'rewrite' => array(
'slug' => 'speaker',
'with_front' => false,),
'menu_icon' => 'dashicons-groups',
'show_in_menu' => 'edit.php?post_type=symposium',
'show_in_rest' => true,
'supports' => array( 'title', 'editor','thumbnail','page-attributes' ));
register_post_type( 'speaker', $args );
$labels = array(
'name' => _x( 'session', 'Post Type General Name', 'gbc' ),
'singular_name' => _x( 'Session', 'Post Type Singular Name', 'gbc' ),
'menu_name' => __( 'Sessions', 'gbc' ),
'name_admin_bar' => __( 'Sessions', 'gbc' ));
$args = array(
'label' => __( 'Sessions', 'gbc' ),
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'menu_icon' => 'dashicons-media-video',
'show_in_menu' => 'edit.php?post_type=symposium'
);
register_post_type( 'session', $args );
}
add_action( 'init', 'my_register_cpt' );
两个部分
此后,我将在此处更改永久链接结构。注意,我现在已经删除了会话重写:
function my_add_rewrite_rules() {
global $wp_rewrite;
// Presentation rewrite
add_rewrite_tag('%presentation%', '([^/]+)', 'presentation=');
add_permastruct('presentation','/%symposium%/presentation/%presentation%', false);
add_rewrite_rule('^presentation/([^/]+)/([^/]+)/?','index.php?presentation=$matches[2]','top');
// Speaker rewrite
add_rewrite_tag('%speaker%', '([^/]+)', 'speaker=');
add_permastruct('speaker', '/%symposium%/speaker/%speaker%', false);
add_rewrite_rule('^speaker/?([^/]+)/([^/]+)/?','index.php?speaker=$matches[2]','top');
$wp_rewrite->flush_rules();
}
add_action( 'init', 'my_add_rewrite_rules' );
三部分
最终在此处的帖子类型过滤器中替换研讨会:
function my_permalinks($permalink, $post, $leavename) {
$post_id = $post->ID;
if($post->post_type != 'speaker' && $post->post_type != 'presentation' && $post->post_type != 'session' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))){
return $permalink;
} else {
$parent = $post->post_parent;
$parent_post = get_post( $parent );
$permalink = str_replace('%symposium%', $parent_post->post_name, $permalink);
return $permalink;
}
}
add_filter('post_type_link', 'my_permalinks', 10, 3);
任何帮助将不胜感激!