plugin polylang - detect the language a post is written in

admin2025-01-07  5

Is there any way to evaluate the language a post/page is written in? I am building a multilingual site and am almost pulling my hair out trying to get the front-end navigation to take the chosen language into account. So far the polylang plugin / has worked fine for everything else.

Is there any way to evaluate the language a post/page is written in? I am building a multilingual site and am almost pulling my hair out trying to get the front-end navigation to take the chosen language into account. So far the polylang plugin http://wordpress.org/extend/plugins/polylang/ has worked fine for everything else.

Share Improve this question edited Feb 11, 2013 at 14:05 Max Yudin 6,3782 gold badges26 silver badges36 bronze badges asked Feb 11, 2013 at 13:30 tembamazingitembamazingi 331 gold badge1 silver badge4 bronze badges 6
  • What do you mean 'evaluate'? Analyze the text and detect the language? Or get the language assigned when post was created? – Max Yudin Commented Feb 11, 2013 at 13:42
  • Get the language assigned when the post/page was created. There will be pages written in, say French and Dutch. If my chosen language is Dutch, I don't want links to the pages in French to be visible. – tembamazingi Commented Feb 11, 2013 at 13:56
  • Simply remove pll_the_languages() from templates or delete the Language Switcher widget. Depends where it comes from. – Max Yudin Commented Feb 11, 2013 at 14:04
  • Tried that to no effect, unfortunately. My menus are still not language-specific. The language is ignored especially when browsing content by category, if that helps diagnose the issue. – tembamazingi Commented Feb 11, 2013 at 14:25
  • If you mean Custom Menus you have to create one menu per language. See Polylang Navigations menus. – Max Yudin Commented Feb 11, 2013 at 14:37
 |  Show 1 more comment

3 Answers 3

Reset to default 3

The main language of a post should be saved in a post meta field. There is no way to detect that automatically. Even Google’s heuristics fail regularly with that.

So add a custom field lang and check with …

$language = get_post_meta( get_the_ID(), 'lang', TRUE );

… what language the post was written in.

Update

Here is a very simple example for a language selector. It will be visible on every post type with a Publish metabox.

get_post_meta( get_the_ID(), '_language', TRUE ); 

… will return the post’s language if available.

add_action( 'post_submitbox_misc_actions', 't5_language_selector' );
add_action( 'save_post', 't5_save_language' );

function t5_language_selector()
{
    print '<div class="misc-pub-section">
        <label for="t5_language_id">Language</label>
        <select name="t5_language" id="t5_language_id">';

    $current = get_post_meta( get_the_ID(), '_language', TRUE );
    $languages = array (
        'en' => 'English',
        'de' => 'Deutsch',
        'ja' => '日本人'
    );

    foreach ( $languages as $key => $language )
        printf(
            '<option value="%1$s" %2$s>%3$s</option>',
            $key,
            selected( $key, $current, FALSE ),
            $language
        );

    print '</select></div>';
}


function t5_save_language( $id )
{
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
        return;

    if ( ! current_user_can( 'edit_post', $id ) )
        return;

    if ( ! isset ( $_POST['t5_language'] ) )
        return delete_post_meta( $id, '_language' );

    if ( ! in_array( $_POST['t5_language'], array ( 'en', 'de', 'ja' ) ) )
        return;

    update_post_meta( $id, '_language', $_POST['t5_language'] );
}

For the rest api, wp-graphql and wp-cli's wp eval-file I needed to do

wp_get_post_terms( $post->ID, 'language' )[0]->slug

The function you need is pll_get_post_language().

Source: https://polylang.pro/doc/function-reference/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736261930a778.html

最新回复(0)