在一个供应商插件中,我得到了以下信息:
register_taxonomy( \'listing-region\', \'listing\', array( \'rewrite\' => false, \'hierarchical\' => true, \'label\' => __( \'Regions\', \'bt_plugin\' ), \'singular_name\' => __( \'Region\', \'bt_plugin\' ), \'show_admin_column\' => true ) );
为了避免锁定插件更新,我想覆盖子主题中的“rewrite”参数以获得以下结果:
https://www.myexample.com/region/foo <;=>https://www.myexample.com?listing-region=foo
我该怎么做?
目前,我在函数中尝试了这一点。php:
function theme_child_custom_rewrite() {
//Ensure the $wp_rewrite global is loaded
global $wp_rewrite;
//Call flush_rules() as a method of the $wp_rewrite object
$wp_rewrite->flush_rules( false );
add_rewrite_tag(\'%listing-region%\',\'([^&]+)\');
add_rewrite_rule(\'^region/(.*?)$\', \'index.php?listing-region=$matches[1]\', \'top\');
}
add_action(\'init\',\'theme_child_custom_rewrite\', 1000);
function listing_region_term_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, \'course\' );
if( $terms ){
return str_replace( \'%listing-region%\' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( \'term_link\', \'listing_region_term_link\', 1, 3 );
最合适的回答,由SO网友:Sally CJ 整理而成
您不需要这些自定义重写和术语永久链接调整,因此只需删除这些和相关回调:
add_action(\'init\',\'theme_child_custom_rewrite\', 1000);
add_filter( \'term_link\', \'listing_region_term_link\', 1, 3 );
只需使用
register_taxonomy_args hook 覆盖分类法的
parameters:
function override_listing_region_taxonomy_args( $args, $taxonomy ) {
if ( \'listing-region\' === $taxonomy ) {
$args[\'rewrite\'] = array(
\'slug\' => \'region\',
);
}
return $args;
}
add_filter( \'register_taxonomy_args\', \'override_listing_region_taxonomy_args\', 11, 2 );
Things to note:
<确保在应用上述代码/更改后刷新永久链接&mdash;只需访问永久链接设置页面(无需进行任何更改或单击提交按钮)。
您可能需要使用更大的优先级值;e、 g。20:
add_filter( \'register_taxonomy_args\', \'override_listing_region_taxonomy_args\', 20, 2 );