Support Log in

12. Custom Taxonomy Translation

Developer Guide
Prefer video? Watch the Lang Forge tutorials 9 short guides covering every feature Watch

Lang Forge supports translating taxonomy terms (categories, tags, and custom taxonomies) through the same translation group mechanism used for posts.

How Taxonomy Translation Works

Each taxonomy term in each language is a separate term in wp_terms. Terms are linked via the wp_lf_translations table with element_type set to the taxonomy name. When Lang Forge filters the main query by language, taxonomy archive pages automatically show only posts in the current language.

Programmatically Translating Taxonomy Terms

php
/**
 * Create a translated term and link it to the original.
 *
 * @param int    $source_term_id Original term ID
 * @param string $taxonomy       Taxonomy slug
 * @param string $target_lang    Target language code
 * @param string $translated_name Translated term name
 * @param array  $args           Optional term args (slug, description, parent)
 * @return int   New term ID
 */
function theme_create_translated_term($source_term_id, $taxonomy, $target_lang, $translated_name, $args = []) {
    $result = wp_insert_term($translated_name, $taxonomy, $args);
    if (is_wp_error($result)) {
        return 0;
    }

    $new_term_id = $result['term_id'];

    // Set the language and link translations
    update_term_meta($new_term_id, '_lf_language', $target_lang);
    update_term_meta($source_term_id, '_lf_language', lf_get_default_language());

    return $new_term_id;
}

// Usage: Create an English translation for a Russian category
$en_term_id = theme_create_translated_term(
    5,                    // Russian term ID
    'category',           // Taxonomy
    'en',                 // Target language
    'Technology',         // English name
    ['slug' => 'technology-en']
);
Example: Query terms by language
php
// Get all categories in the current language
$lang = lf_get_current_language();
$categories = get_terms([
    'taxonomy'   => 'category',
    'hide_empty' => false,
    'meta_query' => [
        [
            'key'   => '_lf_language',
            'value' => $lang,
        ],
    ],
]);

foreach ($categories as $cat) {
    echo '<a href="' . esc_url(get_term_link($cat)) . '">';
    echo esc_html($cat->name);
    echo '</a>';
}
Example: Display a category archive with translated category names
php
// archive.php - Show translated category title
if (is_category()) {
    $term = get_queried_object();
    echo '<h1 class="archive-title">' . esc_html($term->name) . '</h1>';
    echo '<p class="archive-description">' . esc_html($term->description) . '</p>';
}

Forge AI Assistant Online

Hi! I'm the Lang Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs