WordPress Change logo image when I click to a different language

后端 未结 6 899
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-02 19:59

I have a multilingual website. Is there a way I can change the logo.png to a different .png after I switch to \"India\"? I am using polylang plugin

相关标签:
6条回答
  • 2021-01-02 20:10
    function pojo_polylang_get_multilang_logo( $value ) {
    if ( function_exists( 'pll_current_language' ) ) {
        $logos = array(
            'en' => 'logo-en.png',
            'in' => 'logo-in.png',
        );
        $default_logo = $logos['en'];
        $current_lang = pll_current_language();
        $assets_url = get_stylesheet_directory_uri() . '/assets/images/';
        if ( isset( $logos[ $current_lang ] ) )
            $value = $assets_url . $logos[ $current_lang ];
        else
            $value = $assets_url . $default_logo;
    }
    return $value;
    }
    add_filter( 'theme_mod_image_logo', 'pojo_polylang_get_multilang_logo' );
    

    if you have a look from $assets_url to $return $value you will see an if statement :

            $assets_url = get_stylesheet_directory_uri() . '/assets/images/';
        if ( isset( $logos[ $current_lang ] ) )
            $value = $assets_url . $logos[ $current_lang ];
        else
            $value = $assets_url . $default_logo;
    }
    return $value
    

    from what I am looking at I can see an error as the if statement does not include {} maybe if you changed it to the following it might work?

            $assets_url = get_stylesheet_directory_uri() . '/assets/images/';
        if ( isset( $logos[ $current_lang ] ) ) {
            $value = $assets_url . $logos[ $current_lang ];
        } else {
            $value = $assets_url . $default_logo;
        }
    }
    return $value
    
    0 讨论(0)
  • 2021-01-02 20:15

    I don't recommend doing this in PHP since that would invalidate your cache and in turn will affect your performance. Instead you should use JavaScript, JQuery to change the logo based on your lang atribute.

    Edit: It looks like WPML actually allows this by installing the WPML String Translations extension.

    0 讨论(0)
  • 2021-01-02 20:16

    in your code you have set the $default_logo = $logos['en']; and your $current_lang value is also 'en' so when you change the language your $current_lang value need to change as per the selected language's short code otherwise your $default_logo and $value will be the same..

    0 讨论(0)
  • 2021-01-02 20:17

    If your page is SEO Friendly and You change <html lang=''> for different language, then You can change logo on client side by jQuery.

    0 讨论(0)
  • 2021-01-02 20:20

    You can easily register a string called LOGO and put all links (or names) in it!

    Step 1: Add below code in functions.php

    pll_register_string("LOGO","example.com/path/to/images/logo.png"); // this url for default language
    

    Step 2: Now in your wp-admin in "string translations" under Languages, you have "LOGO" field available in all languages.

    Step 3: To get logo URL (in all languages) just use this code:

    <?php pll_e("LOGO"); ?>
    

    Note: if you want put only logo name in this field, your code should be like below:

    <?php 
    $assets_url = get_stylesheet_directory_uri() . '/assets/images/';
    echo $assets_url; pll_e("LOGO"); ?>
    
    0 讨论(0)
  • 2021-01-02 20:28

    We have done almost the same on a blog that contains 14 categories, and each category had to display its own logo.

    In our case, we used custom php code, that checks in the url and overrides the logo display in the theme accordingly, while fetching the logo from the logo path from the db.

    In your case, it should be easier since the language variable is easily accessible, so all you need to do in your theme's header.php is a if statement around the logo while fetching the logo image from the database (preferably from the options table) depending on the language.

    //fetch the current language ( you can use https://polylang.pro/doc/function-reference/)
    
    $current_language = pll_current_language();
    
    //while fetching the logo for the current language, it's best if you add a fallback to the default language in case the option for the current language is not set
    
    $logo_image = get_option("logo_".$current_language,"logo_".$pll_default_language());
    
    ?> <img class="logo" src="<?php echo $logo_image; ?>"
    

    Hope this is what you're looking for.

    0 讨论(0)
提交回复
热议问题