如何在另一个自定义帖子类型中使用自定义帖子类型slug slug
我在WordPress项目中创建了两个自定义帖子类型: City 和属性使用以下查询。
register_post_type('city',
array(
'labels' => array(
'name' => __('City', 'dc_qode'),
'singular_name' => __('City', 'dc_qode'),
),
'public' => true,
'show_in_menu' => true,
'rewrite' => array('slug' => 'city'),
'show_ui' => true,
'has_archive' => false,
'hierarchical' => true,
'show_tagcloud' => false,
'supports' => array(
'title',
'editor',
'thumbnail',
),
'can_export' => true,
'taxonomies' => array( 'city'),
)
);
register_post_type('property',
array(
'labels' => array(
'name' => __('Property', 'dc_qode'),
'singular_name' => __('Property', 'dc_qode'),
),
'public' => true,
'show_in_menu' => true,
'rewrite' => array('slug' => 'property'),
'show_ui' => true,
'has_archive' => false,
'hierarchical' => true,
'show_tagcloud' => false,
'supports' => array(
'title',
'editor',
'thumbnail',
),
'can_export' => true,
'taxonomies' => array( 'property'),
)
);
使用此信息,我可以使用URL http://domain-name.com/property/property-name
访问任何属性。但是我想以http://domain-name.com/city-name/property-name
(例如http://domain-name.com/toronto)访问URL。 /abcproperty
)。城市名称将与每个属性一起分配。我试图在财产中使用slug“城市”,以
'rewrite' => array('slug' => '%city%')
代替
'rewrite' => array('slug' => 'property')
:但行不通。
我该如何达到这种情况?
I have created two custom post types in my wordpress project: city and property using the below query.
register_post_type('city',
array(
'labels' => array(
'name' => __('City', 'dc_qode'),
'singular_name' => __('City', 'dc_qode'),
),
'public' => true,
'show_in_menu' => true,
'rewrite' => array('slug' => 'city'),
'show_ui' => true,
'has_archive' => false,
'hierarchical' => true,
'show_tagcloud' => false,
'supports' => array(
'title',
'editor',
'thumbnail',
),
'can_export' => true,
'taxonomies' => array( 'city'),
)
);
register_post_type('property',
array(
'labels' => array(
'name' => __('Property', 'dc_qode'),
'singular_name' => __('Property', 'dc_qode'),
),
'public' => true,
'show_in_menu' => true,
'rewrite' => array('slug' => 'property'),
'show_ui' => true,
'has_archive' => false,
'hierarchical' => true,
'show_tagcloud' => false,
'supports' => array(
'title',
'editor',
'thumbnail',
),
'can_export' => true,
'taxonomies' => array( 'property'),
)
);
Using this, I can access any property using the url http://domain-name.com/property/property-name
. But I want to access the url as http://domain-name.com/city-name/property-name
(for eg. http://domain-name.com/toronto/abcproperty
). The city-name will be assigned with each property. I tried to use the slug, 'city', within property as:
'rewrite' => array('slug' => '%city%')
in place of
'rewrite' => array('slug' => 'property')
But it's not working.
How can I achieve this scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以发表自定义帖子以拥有像页面这样的父母。因此,您必须要做三件事:
让您自定义帖子支持页面属性。
您必须注册新的重写规则,然后将定义新的permasustruct,该规则将在第三步中使用。
注册了新的重写规则后,您应将此规则应用于自定义帖子的永久链接。
完成工作代码
You can make your custom posts to have parents like page. So there are three main thing you have to do:
Make you custom post to support Page attributes.
You have to register new rewrite rule, which will then define a new permastruct which will be used in the third step.
After registering the new rewrite rule, you should apply this rule to your custom posts' permalinks.
Complete working code
,'index.php?properties=$matches[2]','top' ); }); add_filter( 'post_type_link', function( $link, $post ) { if ( 'properties' == get_post_type( $post ) ) { //Lets go to get the parent city name if( $post->post_parent ) { $parent = get_post( $post->post_parent ); if( !empty($parent->post_name) ) { return str_replace( '%city_name%', $parent->post_name, $link ); } } else { //This seems to not work. It is intented to build pretty permalinks //when properties has not parent, but it seems that it would need //additional rewrite rules //return str_replace( '/%city_name%', '', $link ); } } return $link; }, 10, 2 );