可能最简单的方法之一是venue suburb 非层次自定义分类法。创建场馆帖子时,请为郊区添加一个术语并将其指定给该帖子。register post type rewrite参数包含对%venue_suburb%
标记,然后您可以筛选post_type_link
要将该标签替换为选定的场馆郊区术语:
add_action( \'init\', \'wpa65770_venue_custom_types\' );
function wpa65770_venue_custom_types() {
// extra parameters for tax and post type trimmed to save space
register_taxonomy( \'venue_suburb\',\'venue\',
array(
\'hierarchical\' => false,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'venue-suburb\' ),
)
);
register_post_type( \'venue\',
array(
\'public\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'venue/%venue_suburb%\' ), // rewrite with %venue_suburb%
\'hierarchical\' => false,
\'supports\' => array(\'title\', \'editor\', \'thumbnail\')
)
);
}
add_filter( \'post_type_link\', \'wpa65770_venue_permalink\', 10, 3 );
function wpa65770_venue_permalink( $permalink, $post, $leavename ) {
if ( false !== strpos( $permalink, \'%venue_suburb%\' )
&& $venue_suburb = get_the_terms( $post->ID, \'venue_suburb\' ) ) {
$permalink = str_replace( \'%venue_suburb%\', array_pop( $venue_suburb )->slug, $permalink );
}
return $permalink;
}
创建帖子时,您将看到
%venue_suburb%
标记URL,直到您将一个术语与帖子关联。添加上述代码以刷新重写规则后,请确保访问admin中的permalinks设置页面,以便所有这些都生效。
EDIT- 基于您的代码的其他版本。您必须修改它以从序列化的元中提取郊区,我刚刚用字符串val测试了地址:
add_action(\'init\', \'myposttype_rewrite\');
function myposttype_rewrite() {
global $wp_rewrite;
$queryarg = \'post_type=venue&venue=\';
$wp_rewrite->add_rewrite_tag(\'%suburb%\', \'([^/]+)\', $queryarg);
$wp_rewrite->add_permastruct(\'venue\', \'/my-venues/%suburb%/%venue%/\', false);
}
add_filter(\'post_type_link\', \'venue_permalink\', 1, 3);
function venue_permalink($post_link, $post_obj, $leavename) {
$post = &get_post($post_obj);
if ( is_wp_error( $post ) )
return $post;
$address = get_post_meta( $post_obj->ID, \'address\', true );
$newlink = str_replace( \'%suburb%\', $address, $post_link );
return $newlink;
}