1

In my website I am using WPML for translations. So my website's main language is English, and I am rewrite the permalinks of my cpt (artikelen) to website.com/postname/post-id. This works perfectly for the standard language, but when I use my language switcher to go to French for example, I get a 404 error. This is because the /fr/ is not used in the permalink anymore, because of the rewrite rule in my functions.php. So for another language the permalink for my custom post type should be website.com/fr/postname/post-id. Or for dutch: website.com/nl/postname/post-id. Is there any way to include this in the following code?

add_filter('post_type_link', 'wpse33551_post_type_link', 1, 3);

function wpse33551_post_type_link( $link, $post = 0 ){
    if ( $post->post_type == 'artikelen' ){
        return home_url( $post->post_name .'/'. $post->ID );
    } else {
        return $link;
    }
}

add_action( 'init', 'wpse33551_rewrites_init' );

function wpse33551_rewrites_init(){
    add_rewrite_rule(
        '^([^/]+)/([0-9]+)/?$',
        'index.php?post_type=artikelen&p=$matches[2]',
        'top'
    );
}

Your Answer

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

Browse other questions tagged or ask your own question.