我已经为此挣扎了几个小时了。
我正在尝试获取此URL结构:
example.com/business/%custom-tax-name%/%custom-post-name%/
我所说的海关税名称不是指分类法的名称(“位置”),而是指为特定职位选择的值,即城市名称。所以,
example.com/business/long-island-city/business-name
或
example.com/business/phoenix/business-name/
到目前为止,我已经制定了自定义分类法:
$singular = \'Merchant Location\';
$plural = \'Merchant Locations\';
$taxonomy_args = array(
\'rewrite\' => array(
\'slug\' => \'business-location\',
\'with_front\' => FALSE,
\'hierarchical\' => FALSE
)
);
// Register taxonomy ...
并更改了
post_type_link
滤器
function filter_business_permalinks($post_link, $post, $leavename, $sample) {
if ($post->post_type == \'my_custom_post_type\') {
$terms = get_the_terms($post->ID, \'my_custom_taxonomy\');
foreach ($terms as $term) {
$post_link = str_replace(\'business/\', \'business/\'. $term->slug .\'/\', $post_link);
break;
}
return $post_link;
}
add_filter(\'post_type_link\', \'filter_business_permalinks\', 10, 4);
在那之后,得到的URL很好,这正是我想要的,所以过滤器功能工作得很好。但是,当我单击URL时,它会转到以下位置:
example.com/business/long-island-city/long-island-city/business-name
生成404。
这可能是什么原因造成的,我如何修复它?
非常感谢您的帮助。
Note:
我不想要这样的URL结构:
example.com/business/location/long-island-city/business-name/
在URL中具有分类名称。
SO网友:Milo
这是我用来获得你想要实现的目标的方法。
首先,注册您的位置分类:
register_taxonomy(
\'location\',
array( \'business\' ),
array(
\'rewrite\' => array( \'slug\' => \'business-location\' )
)
);
接下来,注册业务职位类型。这里需要注意的重要一点是
%location%
重写slug中的标记。我们将使用它替换为
post_type_link
功能:
register_post_type(
\'business\',
array(
\'label\' => \'Business\',
\'public\' => true,
\'rewrite\' => array( \'slug\' => \'business/%location%\' ),
\'hierarchical\' => false
)
);
现在,位置术语中要交换的函数:
function wpa_business_permalinks( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) && $post->post_type == \'business\' ){
$terms = wp_get_object_terms( $post->ID, \'location\' );
if( $terms ){
return str_replace( \'%location%\' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( \'post_type_link\', \'wpa_business_permalinks\', 1, 2 );