Last active
February 13, 2020 18:38
-
-
Save willybahuaud/ddab1a110fd0d494239f to your computer and use it in GitHub Desktop.
Translate Taxonomy base slug wpml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'init', 'register_my_taxo' ); | |
function register_my_taxo() { | |
register_taxonomy( 'immo_cat', 'immobilier', array( | |
'hierarchical' => false, | |
'label' => 'Type de biens', | |
// je déclare le slug de taxo de manière à pouvoir le traduire | |
'rewrite' => array( 'slug' => icl_t('nebula', 'property-type-slug', 'type-bien-immobilier' ) ), | |
) ); | |
} | |
// je le traduit | |
add_action( 'init', 'register_slugs' ); | |
function register_slugs() { | |
icl_register_string('nebula', 'property-type-slug', 'type-bien-immobilier' ); | |
} | |
add_filter( 'rewrite_rules_array', 'wabeo_rewrite_rules', 99 ); | |
function wabeo_rewrite_rules( $aRules ){ | |
// je fais une pirouette pour faire ressortir les contenus sur ces URLS | |
// property-type is my transalted slug | |
$aNewRules = array( 'property-type/([^/]+)/?$' => 'index.php?immo_cat=$matches[1]' ); | |
$bNewRules = array( 'property-type/([^/]+)/page/([^/]+)/?$' => 'index.php?immo_cat=$matches[1]&paged=$matches[2]' ); | |
$aRules = $aNewRules + $bNewRules + $aRules; | |
return $aRules; | |
} | |
//J'affiche le bon slug dans les terms, en fonciton de la langue | |
add_filter( 'term_link', 'willy_term_link', 999, 3 ); | |
function willy_term_link( $term_link, $term, $taxonomy ) { | |
if ( $taxonomy == 'immo_cat' ) { | |
if ( icl_object_id( $term->term_id, 'immo_cat', false, 'en' ) == $term->term_id ) { | |
$term_link = str_replace( 'type-bien-immobilier', 'property-type', $term_link ); | |
} else { | |
$term_link = str_replace( 'property-type', 'type-bien-immobilier', $term_link ); | |
} | |
} | |
return $term_link; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Merci beaucoup!