使用ACF字段值的自定义帖子类型的多层永久链接
我有一个客户站点,上面有几种自定义帖子类型 - 地区,县,城市,社区。我想为它们设置动态生成的永久链接结构,如下所示:
/[region]
/[region]/[county]
/[region]/[county]/[county]
/[action]/[region]/[county]/[县]/[县] /[社区]
我为POST_TYPE_LINK添加了一个过滤器,并传递了一个正在更新永久链接的函数:
add_filter('post_type_link', array(&$this, 'rewriteCPTpermalinks'), 10, 2);
/**
* Rewrite Custom Post Type Permalinks
*
* @return void
*/
public function rewriteCPTpermalinks( $url, $post ){
global $wp_rewrite;
if ($post->post_type === 'counties') {
$regionObject = get_field('region', $post->ID );
// $url = get_the_permalink($regionObject->ID).$post->post_name;
$url = str_replace( '%region%', get_the_permalink($regionObject->ID), $url );
}
if ($post->post_type == 'cities') {
$countyObject = get_field('county', $post->ID);
$regionObject = get_field('region', $countyObject->ID );
$url = get_the_permalink($regionObject->ID).$countyObject->post_name.'/'.$post->post_name;
}
if ($post->post_type == 'communities') {
$cityObject = get_field('city_post', $post->ID);
$countyObject = get_field('county', $cityObject->ID);
$regionObject = get_field( 'region', $countyObject->ID );
$url = $regionObject->post_name.$countyObject->post_name.'/'.$cityObject->post_name.'/'.$post->post_name;
}
return $url;
}
这是在更新永久链接,但我将为所有帖子提供404页。我在设置中保存了永久链接>永久链接和尝试flush_rewrite_rules()
,但似乎无法使这些工作。
这样做的关键是要使事情自动起作用,而不必从每个帖子中选择自定义分类法。
我不确定我是否在这里使用正确的方法,但感谢任何帮助。
I have a client site with several custom post types - region, county, city, community. I want to set up dynamically generated permalink structures for them as follows:
/[region]
/[region]/[county]
/[region]/[county]/[city]
/[region]/[county]/[city]/[community]
I have added a filter for post_type_link and am passing in a function that is updating the permalinks:
add_filter('post_type_link', array(&$this, 'rewriteCPTpermalinks'), 10, 2);
/**
* Rewrite Custom Post Type Permalinks
*
* @return void
*/
public function rewriteCPTpermalinks( $url, $post ){
global $wp_rewrite;
if ($post->post_type === 'counties') {
$regionObject = get_field('region', $post->ID );
// $url = get_the_permalink($regionObject->ID).$post->post_name;
$url = str_replace( '%region%', get_the_permalink($regionObject->ID), $url );
}
if ($post->post_type == 'cities') {
$countyObject = get_field('county', $post->ID);
$regionObject = get_field('region', $countyObject->ID );
$url = get_the_permalink($regionObject->ID).$countyObject->post_name.'/'.$post->post_name;
}
if ($post->post_type == 'communities') {
$cityObject = get_field('city_post', $post->ID);
$countyObject = get_field('county', $cityObject->ID);
$regionObject = get_field( 'region', $countyObject->ID );
$url = $regionObject->post_name.$countyObject->post_name.'/'.$cityObject->post_name.'/'.$post->post_name;
}
return $url;
}
This is updating the permalinks but I am getting a 404 page for all posts. I have saved permalinks in Settings > Permalinks and tried flush_rewrite_rules()
but cannot seem to get these working.
The key for this is to have things work automatically without having to choose from a custom taxonomy for every post.
I'm not sure if I am using the right approach here but would appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论