Last active
April 3, 2018 21:18
-
-
Save willybahuaud/3b84dd2e267d093595a7686f10ce5a22 to your computer and use it in GitHub Desktop.
Préfixer les articles par l’URL du page for posts (avec WPML)
This file contains 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_filter( 'post_link', 'w_post_link', 10, 3 ); | |
function w_post_link( $url, $post, $leavename=false ) { | |
if ( $post->post_type == 'post' ) { | |
global $sitepress; | |
$lang = apply_filters( 'wpml_post_language_details', 'fr', $post->ID ); | |
$base = get_permalink( icl_object_id( get_option( 'page_for_posts' ), 'page', true, $lang['language_code'] ) ); | |
$url = str_replace( $sitepress->language_url( $lang['language_code'] ), $base, $url ); | |
} | |
return $url; | |
} | |
add_filter( 'post_rewrite_rules', 'w_post_rewrite_rules' ); | |
function w_post_rewrite_rules( $rules ) { | |
global $sitepress; | |
foreach ( apply_filters( 'wpml_active_languages', '', array( 'skip_missing' => 0 ) ) as $l => $lang ) { | |
$base = get_permalink( icl_object_id( get_option( 'page_for_posts' ), 'page', true, $l ) ); | |
$url = str_replace( $sitepress->language_url( $l ), '', $base ); | |
$rules[ $url . '([^/]+)(?:/([0-9]+))?/?$'] = 'index.php?name=$matches[1]&page=$matches[2]'; | |
} | |
return $rules; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ce code a été écrit vite fait, pour les besoins d’un projet, et peut être amélioré :)