admin管理员组文章数量:1026989
I have been working on this for quite awhile but I'm stuck at rewriting the permalinks for both the parent and the child.
So I created two custom post types: magazines
and magazine-pages
both MUST be hierarchical. Also magazine-pages
is part of the magazine
custom post type:
Here's my code for that:
// Magazines
$args = array(
'label' => 'Magazines',
'labels' => array(
//my-labels
),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 25,
'hierarchical' => true,
'query_var' => true,
'rewrite' => array( "slug" => "magazines" ),
'supports' => array('title', 'thumbnail'),
'menu_icon' => 'dashicons-book'
);
register_post_type('magazines', $args);
// Magazine pages
$args = array(
'label' => 'Magazine pages',
'labels' => array(
//my-labels
),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'hierarchical' => true,
'query_var' => true,
"rewrite" => array( "slug" => "magazines" ),
'supports' => array('title', 'thumbnail', 'editor'),
'show_in_menu' => 'edit.php?post_type=magazines'
);
register_post_type('magazine-pages', $args);
}
What I'm trying to achieve:
/magazines/magazine-1/magazine-page-1
Where
/magazine-1 Should be a post from CPT
magazine
(parent)
and
/magazine-page-1 Should be a post from CPT
magazine-pages
(child)
I did add the following rewrite rules to make this work, but unfortunately it doesn't. Only the parent page works, the child pages returns a 404 error.
add_action( 'init', function() {
add_rewrite_rule( '^magazines/([^/]*)/?','index.php?magazines=$matches[1]','top' );
add_rewrite_rule( '^magazines/([^/]*)/([^/]*)/?','index.php?magazine-pages=$matches[2]','top' );
});
Did I do something wrong in the rewrite rules? All the help is welcome!
I have been working on this for quite awhile but I'm stuck at rewriting the permalinks for both the parent and the child.
So I created two custom post types: magazines
and magazine-pages
both MUST be hierarchical. Also magazine-pages
is part of the magazine
custom post type:
Here's my code for that:
// Magazines
$args = array(
'label' => 'Magazines',
'labels' => array(
//my-labels
),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 25,
'hierarchical' => true,
'query_var' => true,
'rewrite' => array( "slug" => "magazines" ),
'supports' => array('title', 'thumbnail'),
'menu_icon' => 'dashicons-book'
);
register_post_type('magazines', $args);
// Magazine pages
$args = array(
'label' => 'Magazine pages',
'labels' => array(
//my-labels
),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'hierarchical' => true,
'query_var' => true,
"rewrite" => array( "slug" => "magazines" ),
'supports' => array('title', 'thumbnail', 'editor'),
'show_in_menu' => 'edit.php?post_type=magazines'
);
register_post_type('magazine-pages', $args);
}
What I'm trying to achieve:
/magazines/magazine-1/magazine-page-1
Where
/magazine-1 Should be a post from CPT
magazine
(parent)
and
/magazine-page-1 Should be a post from CPT
magazine-pages
(child)
I did add the following rewrite rules to make this work, but unfortunately it doesn't. Only the parent page works, the child pages returns a 404 error.
add_action( 'init', function() {
add_rewrite_rule( '^magazines/([^/]*)/?','index.php?magazines=$matches[1]','top' );
add_rewrite_rule( '^magazines/([^/]*)/([^/]*)/?','index.php?magazine-pages=$matches[2]','top' );
});
Did I do something wrong in the rewrite rules? All the help is welcome!
本文标签: url rewritingCustom Post Type parentchild relationship rewrite rules for permalinks
版权声明:本文标题:url rewriting - Custom Post Type parentchild relationship rewrite rules for permalinks 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745655251a2161560.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论