0

( i open a new question since i found why my slug don't translate in generated links, sorry if it is an error, but sounds logical for me ).

so i use WPML and Custom Posts Type on my wordpress.

i also add .html to my CPT with a code found here on stackexchange.

// .html pour les customs posts

function rewrite_rules($rules) {
    $new_rules = array();
    foreach (get_post_types() as $t)
    $new_rules[$t . '/(.+?)\.html$'] = 'index.php?post_type=' . $t . '&name=$matches[1]';
    return $new_rules + $rules;
}
add_action('rewrite_rules_array', 'rewrite_rules');

function custom_post_permalink ($post_link) {
    global $post;
    if ( $post ) {
        $type = get_post_type($post->ID);
        return home_url() . '/' . $type . '/' . $post->post_name . '.html';
    }
}
add_filter('post_type_link', 'custom_post_permalink'); // for cpt post_type_link (rather than post_link)

BUT

with this function, then my generated URLS dont use translated slug, but default language slug and goes to 404.

How can i edit this code to get the correct WPML slug for CPT ?

if i take of this code, it works perfect, but without .html at the end of my URLS.

Thanks a lot for your precious help

0

Aleluhia !

it was because $type don't use WPML function

Here is the code that works perfectly with WPML and translated slug :

// .html pour les customs posts

function rewrite_rules($rules) {
    $new_rules = array();
    foreach (get_post_types() as $t)
    $new_rules[$t . '/(.+?)\.html$'] = 'index.php?post_type=' . $t . '&name=$matches[1]';
    return $new_rules + $rules;
}
add_action('rewrite_rules_array', 'rewrite_rules');

function custom_post_permalink ($post_link) {
    global $post;
    if ( $post ) {
        $type = get_post_type($post->ID);
        $slug = apply_filters( 'wpml_get_translated_slug',$type,$type,'');
        return home_url() . '/' . $slug . '/' . $post->post_name . '.html';
    }
}
add_filter('post_type_link', 'custom_post_permalink'); // for cpt post_type_link (rather than post_link)
  • just for information, there is a double $type in $slug. this is because of same parametre for "register_post_type" & "rewrite" option. Change if not the same at your place. – Gregory Mar 21 '18 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.