-
-
Save stevygee/ed18db68a247c2cd4fb7680d39d41502 to your computer and use it in GitHub Desktop.
| <?php | |
| function sg_get_translated_posts_urls() { | |
| $default_lang = apply_filters( 'wpml_default_language', NULL ); | |
| $ids = get_posts( array( | |
| 'fields' => 'ids', | |
| 'post_type' => array( 'post', 'page' ), | |
| 'numberposts' => -1, | |
| 'suppress_filters' => true, // Get posts in all languages | |
| ) ); | |
| $permalinks = array(); | |
| foreach ( $ids as $id ) { | |
| $lang = apply_filters( 'wpml_post_language_details', NULL, $id ); | |
| // We only need translations; skip posts in default language | |
| if ( $lang['language_code'] === $default_lang ) { | |
| continue; | |
| } | |
| $permalink = get_permalink( $id ); | |
| $wpml_permalink = apply_filters( 'wpml_permalink', $permalink, $lang['language_code'] ); | |
| // Strip home URL (domain) from permalink | |
| $wpml_permalink = str_replace( trailingslashit( home_url() ), '/', $wpml_permalink ); | |
| $permalinks[] = $wpml_permalink; | |
| } | |
| return $permalinks; | |
| } | |
| function sg_add_additional_urls( $url_queue ) { | |
| /* Add WPML translations */ | |
| $translation_urls = sg_get_translated_posts_urls(); | |
| $url_queue = array_merge( | |
| $url_queue, | |
| $translation_urls | |
| ); | |
| return $url_queue; | |
| } | |
| add_filter( 'wp2static_modify_initial_crawl_list', 'sg_add_additional_urls' ); |
Expected: I wish to make content appear at translated URL
/es/contacto/which is for Spanish version of Contact page.
I think this issue can be solved by:
$wpml_permalink = apply_filters( 'wpml_permalink', $permalink, $lang['language_code'] );
→
$wpml_permalink = apply_filters( 'wpml_permalink', $permalink, $lang['language_code'], true );
See $full_resolution parameter at https://wpml.org/wpml-hook/wpml_permalink/.
@stevygee thanks for sharing! I have an idea for improvement.
$posts = get_posts( array(
'post_type' => array( 'post', 'page' ),
'numberposts' => -1,
'suppress_filters' => true, // Get posts in all languages
) );
$ids = wp_list_pluck( $posts, 'ID' );
→
$ids = get_posts( array(
'fields' => 'ids',
'post_type' => array( 'post', 'page' ),
'numberposts' => -1,
'suppress_filters' => true, // Get posts in all languages
) );
@fertek You are right, thanks for the simplification!
Edited: After all I was able to get it working using fertek's suggestion above using the true parameter after all. For this to work reliably, you need to go to WPML > Languages > and disable "Adjust IDs for multilingual functionality". Otherwise, the IDs get converted and you will be missing some pages during the crawl.
Thank you for sharing these solutions.
@bkno Glad to hear that it works for you now!
@thegulshankumar Two things come to mind:
/es/contacto/when accessing WordPress directly? If not, try to get the permalinks working first.