WPML
状态
已由作者解决
解决于
BetterDocs 4.2.3
相关插件/主题
BetterDocs
主题标签
Compatibility

问题概述

BetterDocs 与 WPML 结合使用时,您可能会遇到以下问题:通过 BetterDocs 拖放界面重新排序文档会导致类别分配在次要语言中变得不同步。因此,文档可能会被错误地分配到其他语言的类别中,并且新的顺序可能无法在翻译中反映出来。

临时解决方法

在继续操作之前,请确保为您的站点创建完整的站点备份


  • 打开 …wp-content/plugins/betterdocs/includes/Core/PostType.php 文件。

  • 找到第 599 行。

  • 将:
    if ( update_term_meta( $term_id, '_docs_order', $docs_ordering_data ) ) {
        wp_send_json_success( __( 'Successfully updated.', 'betterdocs' ) );
    }
            


  • 替换为:
    if ( update_term_meta( $term_id, '_docs_order', $docs_ordering_data ) ) {
        // WPML Integration: Sync taxonomies after order change
        if ( class_exists( 'SitePress' ) && class_exists( 'WPML_Term_Translation_Utils' ) ) {
            $docs_array = isset( $_POST['docs_ordering_data'] ) && is_array( $_POST['docs_ordering_data'] ) 
                ? array_map( 'intval', $_POST['docs_ordering_data'] )
                : explode( ',', $docs_ordering_data );
            $this->wpml_sync_posts_taxonomies( $docs_array );
        }
        
        wp_send_json_success( __( 'Successfully updated.', 'betterdocs' ) );
    }
            


  • 在同一文件的末尾,即最后一个 } 之前(大约第 1130 行)。

  • 添加以下辅助函数:
    /**
     * WPML Integration: Sync post taxonomies across translations
     * @param array $post_ids Array of post IDs to sync
     */
    private function wpml_sync_posts_taxonomies( $post_ids ) {
        global $sitepress;
        if ( ! $sitepress || empty( $post_ids ) ) return;
    
        $sync = new WPML_Term_Translation_Utils( $sitepress );
        
        foreach ( $post_ids as $post_id ) {
            $trid = $sitepress->get_element_trid( $post_id, 'post_docs' );
            if ( ! $trid ) continue;
            
            $translations = $sitepress->get_element_translations( $trid, 'post_docs' );
            $original_id = $post_id;
            
            foreach ( $translations as $translation ) {
                if ( isset( $translation->original ) && $translation->original == 1 ) {
                    $original_id = $translation->element_id;
                    break;
                }
            }
            
            foreach ( $sitepress->get_active_languages() as $lang => $details ) {
                $sync->sync_terms( $original_id, $lang );
            }
        }
    }
            

所有已知问题 →