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

    (@ceceppa)

    Hi,
    of course. You can write:

    /**
       * get the translation id, if exists, in selected language
       *
       * this function will return 0 if no translation is found.
       *
       * @param int/string $lang - language id/slug in which return translation
       * @param int $post_id - post id
       *
       * @return int
       */
    CMLPost::get_translation( 'fr', $nl_id );

    There is also the function to retrieve all translation of a post:

    CMLPost::get_translations(...)

    The full API documentation is available with the plugin in “Ceceppa Multilingua” -> “Api” page or on my website.

    If you need something else, let me know ??

    Thread Starter romeovs

    (@romeovs)

    Yes well actually.

    My client wants me to build a homepage were the location of certain pages is fixed.
    So I need to get pages by slug.

    I hoped I could just give the slug in english and then check if there are translations available.

    $post = get_posts(array(
       'name' => $slug,
       'post_type' => 'any'
    ))[0];
    $id = $post->ID;
    $id = get_translation(cml_get_current_language()->cml_language_slug, $id);

    This doesn’t work however because the slugs are different for each language and somehow get_posts only returns a post if it is found to be in the current language.

    is there a way around this (eg use get_posts to find all posts in any language, or translate the slug automatically)?

    Plugin Author Alessandro Senese

    (@ceceppa)

    You can pass the parameter “lang” to get_posts to force filtering to certain language, bat if you pass on no language filtering will be applied.

    In alternative you can:

    $page = get_page_by_path($slug);
    
    //The following function will return an array with all translations found
    $translations = CMLPost::get_translations( $page->ID );
    
    print_r( $translations );

    Thread Starter romeovs

    (@romeovs)

    Ok thanks! very nice indeed that extra parameter ‘lang’

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Find the translation of a post or page by id’ is closed to new replies.