admin管理员组

文章数量:1122846

I am trying to retrieve in the "content composer" the categories of projects based on the active language. But i only get the categories off the main language, never on the other.

My code is:

Category All

    $categories = get_categories('taxonomy=jw_portfolio_categories');       

                    if(!empty($categories)){
                        foreach($categories as $cat){
                            ?><option value="<?php echo $cat->term_id; ?>"><?php echo $cat->name; ?></option><?php
                        }
                    }   
    ?>
                </select>

Any idea on how to get all the categories or the ones of the active language in the WP backoffice?

I am trying to retrieve in the "content composer" the categories of projects based on the active language. But i only get the categories off the main language, never on the other.

My code is:

Category All

    $categories = get_categories('taxonomy=jw_portfolio_categories');       

                    if(!empty($categories)){
                        foreach($categories as $cat){
                            ?><option value="<?php echo $cat->term_id; ?>"><?php echo $cat->name; ?></option><?php
                        }
                    }   
    ?>
                </select>

Any idea on how to get all the categories or the ones of the active language in the WP backoffice?

Share Improve this question asked Jun 12, 2012 at 19:39 Pedro FerreiraPedro Ferreira 11 silver badge2 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 0

I have a solution if you are using WPML for the multi-language support, i have used this code before. Modified after your taxonomy.

<?php

//Get categories from current language
//Uses WPML Multi-language

$terms = get_terms('jw_portfolio_categories','hide_empty=1' );

if ( !empty( $terms ) ) {

$term_links = "";

foreach ( $terms as $term ) {

    if( $term->term_id == icl_object_id( $term->term_id,'jw_portfolio_categories', false, ICL_LANGUAGE_CODE ) ) { 
    ?>

        <option value="<?php echo $term->id; ?>"><?php echo $term->name; ?></option>

    <?php
    }
}

echo join( '', $term_links );

}

?>

UPDATED, this should work.

<?php
  $category_ID = get_cat_ID ("CategoryName");
  $get_correct_ID_lang = icl_object_id($category_ID, 'category', false)
  $categories = get_categories('child_of=' . $get_correct_ID_lang);
  foreach ($categories as $cat) {
?>
    <a href="<?php echo get_category_link($cat->cat_ID); ?>"><?php echo $cat->cat_name; ?></a>, 
<?php
  }
?>

You have to translate your post type and also has to create translated categories for other languages too.

本文标签: getcategories only getting categories in the main language