• I’m trying to create a theme that comes with a few predefined taxonomies and taxonomy terms.

    Polylang uses metadata to store information about each element’s language, as well as about the IDs of each element’s translations.

    So, one way to programmatically predefine terms and their translations in a theme seems to be via explicit calls to add_metadata() – but that seems very hacky.

    Does (or will) polylang provide an API to set up languages, taxonomy terms, and their translations, via high-level function calls?

    https://www.ads-software.com/extend/plugins/polylang/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Chouby

    (@chouby)

    To set the language, you can use:

    global $polylang;
    $polylang->set_term_language($term_id, $lang); // $lang is either the language slug or the language term id

    To set translations, you can use:

    $polylang->save_translations('term', $term_id, $translations);
    // $translations is an array with language slugs as keys and term ids as value

    Matt Gibson

    (@gothickgothickorguk)

    Hi,

    Thanks for your answer.

    I’m trying to do exactly the same thing, but not having much success, possibly because I’m not understanding what I should put in the last parameter to save_translations, which you say is an array with language slugs and term ids?

    Is there any chance you could give a simple example, say of adding a single term and its translation?

    Thanks,

    Matt

    Plugin Author Chouby

    (@chouby)

    Say you have an English term with term id 10, and a French term with term id 11. You can tell Polylang that the first is the translation of the second with:

    global $polylang;
    $polylang->save_translations('term', 10, array('en' => 10, 'fr' => 11));

    Matt Gibson

    (@gothickgothickorguk)

    Thanks! It’s become a lot clearer now I’ve experimented with how terms are translated in polylang.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Feature: Adding Taxonomy Terms Programmatically’ is closed to new replies.