Skip to content

Instantly share code, notes, and snippets.

@willybahuaud
Last active April 3, 2018 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willybahuaud/3b84dd2e267d093595a7686f10ce5a22 to your computer and use it in GitHub Desktop.
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)
<?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;
}
@willybahuaud
Copy link
Author

willybahuaud commented Apr 3, 2018

Ce code a été écrit vite fait, pour les besoins d’un projet, et peut être amélioré :)

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