0

To show terms in Bengali I enabled term meta fields, saving data into options table. I made a custom function (get_term_meta()) similar to post_meta to fetch my taxonomy meta data, that takes term_id and the term_meta_key as parameters. How can I use a function so that I can filter my default term titles with the term meta I defined. I tried:

function bn_term_title() {
    var_dump($term);
    /*if( get_locale() === 'bn_BD' )
        return get_term_meta( $term->term_id, 'term_bn' );*/
}
add_filter( 'single_term_title', 'bn_term_title' );

But var_dump( $term ); returns null. How can I get the term_id inside the function?

P.S.: get_term_meta() acts like: get_option( "option_key_name{$term_id}" );

1 Answer 1

0

Solved the issue by passing the term_name as a parameter to the callback function to the filter.

function bn_term_title( $term_name ) {
    $term_types = get_term_by( 'name', $term_name, 'tax_1' );
    if( $term_types->taxonomy === 'tax_1' ) {
        if( get_locale() === 'bn_BD' )
            return get_term_meta( $term_types->term_id, 'tax1_bn' );
        else
            return $term_name;
    }

    $term_categories = get_term_by( 'name', $term_name, 'tax_2' );
    if( $term_categories->taxonomy === 'tax_2' ) {
        if( get_locale() === 'bn_BD' )
            return get_term_meta( $term_categories->term_id, 'tax2_bn' );
        else
            return $term_name;
    }
}
add_filter( 'single_term_title', 'bn_term_title' );

The code can be more concise when get_term_by() will make the $taxonomy parameter optional. If the taxonomy parameter become optional we can do it like below:

function bn_term_title( $term_name ) {
    $term_types = get_term_by( 'name', $term_name );
    if( get_locale() === 'bn_BD' )
        return get_term_meta( $term_types->term_id, 'term_bn' );
    else
        return $term_name;
}
add_filter( 'single_term_title', 'bn_term_title' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.