【问题标题】:Wordpress WPML plugin , get_terms returns data only for current language , i want it return for all available languages?Wordpress WPML 插件,get_terms 仅返回当前语言的数据,我希望它返回所有可用语言的数据吗?
【发布时间】:2021-02-05 11:56:17
【问题描述】:

我正在使用 WPML Plugin v 4.4.9 和 Wordpress v5.6.1

我希望 get_terms 函数返回所有可用语言的结果,例如:对于 category 分类法(使用一些自定义元),它只返回当前的一个结果语言,我怎样才能让它返回所有语言的结果?

例如:

    $args = array(
    'suppress_filter' => true,
    'hide_empty' => false,
    //'meta_query' => $meta_queries,
    );
    
    $terms = get_terms($taxonomy='category',$args);

【问题讨论】:

    标签: php wordpress plugins wpml


    【解决方案1】:

    这里是如何,我在这里节省你的时间, 我花了 4 个该死的小时浏览插件和 wpml 论坛...

    # please note is using category taxonomy by default, 
    # you can specify other though , by calling the function with 2 params.
    
    function get_terms_all_langs($taxonomy='category',$args=array()){
    
    #based on menu troubleshooting.php
    #https://developer.wordpress.org/reference/classes/wp_meta_query/
    #https://wpml.org/forums/topic/remove-get_terms-filters/
    
    global $sitepress;
    
    $has_get_terms_args_filter = remove_filter( 'get_terms_args', array( $sitepress, 'get_terms_args_filter' ) );
    $has_get_term_filter       = remove_filter( 'get_term', array( $sitepress, 'get_term_adjust_id' ), 1 );
    $has_terms_clauses_filter  = remove_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ) );
        
    $terms = get_terms( $taxonomy , $args );
        
    if ( $has_terms_clauses_filter ) {
        add_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ), 10, 3 );
    }
    if ( $has_get_term_filter ) {
        add_filter( 'get_term', array( $sitepress, 'get_term_adjust_id' ), 1, 1 );
    }
    if ( $has_get_terms_args_filter ) {
        add_filter( 'get_terms_args', array( $sitepress, 'get_terms_args_filter' ), 10, 2 );
    }
    
    return $terms;
    }
    

    将上述函数排序为array[lang]块,函数示例:

    function group_cats_by_lang(){
    
    $args = array('suppress_filter' => true,'hide_empty' => false);
    $all_cats = get_terms_all_langs('category',$args); 
    
    $ro_cats = array();
    $fr_cats = array();
    $en_cats = array();
    
    foreach($all_cats as $cat_obj){
    
    $language_code = apply_filters( 'wpml_element_language_code', null, array( 'element_id'=> (int)$cat_obj->term_id, 'element_type'=> 'category' ) );
    
    if($language_code=="ro"){
    $ro_cats['ro'][] = $cat_obj;
    }
    
    if($language_code=="fr"){
    $fr_cats['fr'][] = $cat_obj;
    }
    
    if($language_code=="en"){
    $en_cats['en'][] = $cat_obj;
    }
    
    
    }
    
    return array($ro_cats,$fr_cats,$en_cats);
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多