【发布时间】:2019-02-21 16:20:46
【问题描述】:
我刚刚创建了一个名为 'mtl_chapter' 的自定义帖子类型。使用我的脚本,我将我的 CPT 的帖子父级分配给常规的“帖子”类型。所以,我的CPT基本上是我常规帖子的孩子。我想从
更改我的 CPT 的永久链接结构/base-slug/cpt-post-title/
到
/parent-title/cpt-post-title/
所以,它看起来就像附件帖子与永久链接一样:
/parent-title/attachment-post-title/
我当前的代码能够将永久链接结构更改为我想要的结构,但我得到了
404 未找到
当我点击链接时。请帮助我,这是我当前的代码:
function create_posttype() {
register_post_type( 'mtl_chapter',
array(
'labels' => array(
'name' => 'Chapters',
'singular_name' => 'Chapter',
'parent_item_colon' => 'Novel Title:',
'add_new' => _x('Add New', 'indomtl'),
'add_new_item' => __( 'Add New Chapter' )
),
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-format-aside',
'rewrite' => array('slug' => '%parent-post-name%','with_front' => true),
'exclude_from_search' => true,
'show_ui' => true,
'menu_position' => 5
)
);
}
add_action( 'init', 'create_posttype' );
add_filter('post_type_link', 'mtl_update_permalink_structure', 10, 2);
function mtl_update_permalink_structure( $post_link, $post )
{
if ( false !== strpos( $post_link, '%parent-post-name%' ) ) {
$parent_id = wp_get_post_parent_id($post->ID);
$parent_post = get_post($parent_id);
$slug = $parent_post->post_name;
if ( $slug ) {
$post_link = str_replace( '%parent-post-name%', $slug, $post_link );
}
}
return $post_link;
}
【问题讨论】:
标签: php wordpress url-rewriting permalinks