【发布时间】:2016-03-29 23:48:15
【问题描述】:
我创建了一个自定义帖子类型,并进行了重写,以使用祖父母关系作为 URL,如下所示:
function cpt_child(){
$args = array(
//code
'rewrite' => array( 'slug' => '%grandparent%/%parent%', 'with_front' => false),
);
register_post_type( 'child', $args );
}
add_action( 'init', 'cpt_child' );
然后我更新永久链接:
add_filter( 'post_type_link', 'filter_the_post_type_link', 1, 2 );
function filter_the_post_type_link( $post_link, $post ) {
switch( $post->post_type ) {
case 'child':
$post_link = get_bloginfo( 'url' );
$relationship_child = p2p_type('children_to_parents')->get_adjacent_items($post->ID);
$parent = $relationship['parent']->post_name;
$relationship_parent = p2p_type('parents_to_grandparents')->get_adjacent_items($parent['parent']->ID);
$grandparent = $relationship_parent['parent']->post_name;
$post_link .= "/{$grandparent}/";
$post_link .= "{$parent}/";
$post_link .= "{$post->post_name}";
break;
}
return $post_link;
}
这一切都很好,但不幸的是,重写规则也匹配常规页面,这使得它们成为 404。
我可以通过添加自定义 slug 来防止这种情况,例如“relationship”:http://example.com/relationship/grandparent/parent/child
但我真的很想使用http://example.com/grandparent/parent/child 并且它不会破坏常规页面。
我可能不得不求助于使用 htaccess 的非原生 Wordpress 重写。
提前致谢!
【问题讨论】:
标签: php regex wordpress .htaccess custom-post-type