Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Translate Taxonomy base slug wpml
<?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;
}
@Thyme1152

This comment has been minimized.

Copy link

@Thyme1152 Thyme1152 commented Apr 27, 2018

Merci beaucoup!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment