Jump to content
Sign in to follow this  
dil_bert

WP-Polylang: can i echo a language's name by working with the 'raw' attribute?

Recommended Posts

hello dear freaks,

pretty new to Polylang the translation-plugin for wordpress. 

at the moment i am working out the layout for the new project-page: well I'd like to make a language-switcher at the wordpress-site, but - to be frank:  the default that come shipped with the wordpreess-options they actually wont work for me. I_ am trying to figure out why this is so!? At the moment - and with reagrd to the reference-page of polylang

 

 -  cf: https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/

i think that i can do it with a creation of a function: Therefore i think i can make use of the so called 'raw' attribute. with the first tests i try to figure out whether my languages will show up at all:


$translations = pll_the_languages(array('raw'=>1));
echo $translations[0]['name'];

well - to be frank:  This code doesn't output anything - i guess that I am missing a bit of information?


guess that i should use 

print_r($translations);

 

could i do it like so - and using code like this:

 


echo $translations['it']['name'];

## -- since it seems to be much better to verify if the langauge-key exists in the array or not.


$value= "";
if($key_exists('it',$translations) && $key_exists('name',$translations['nl'])){
$value = $translations['it']['name'];
}
echo $value;


for the reference-things i should have to look at the polylang reference-page here - cf https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/


when using one or more of these function, you *must* check for its existence before using it, otherwise your site will badly break with a fatal error at next Polylang update (as WordPress deletes the plugin when updating it).

pll_the_languages
Displays a language switcher.

Usage:


pll_the_languages( $args );
$args is an optional array parameter

.

Options are:


‘dropdown’ => displays a list if set to 0, a dropdown list if set to 1 (default: 0)
‘show_names’ => displays language names if set to 1 (default: 1)
‘display_names_as’ => either ‘name’ or ‘slug’ (default: ‘name’)
‘show_flags’ => displays flags if set to 1 (default: 0)
‘hide_if_empty’ => hides languages with no posts (or pages) if set to 1 (default: 1)
‘force_home’ => forces link to homepage if set to 1 (default: 0)
‘echo’ => echoes if set to 1, returns a string if set to 0 (default: 1)
‘hide_if_no_translation’ => hides the language if no translation exists if set to 1 (default: 0)
‘hide_current’=> hides the current language if set to 1 (default: 0)
‘post_id’ => if set, displays links to translations of the post (or page) defined by post_id (default: null)
‘raw’ => use this to create your own custom language switcher (default:0)
Important: You have to output yourself the ul tags if you don’t use the dropdown option.

 

Examples:

<!-- outputs a list of languages names -->
<ul>
<?php pll_the_languages(); ?>
</ul>
 
<!-- outputs a flags list (without languages names) -->
<ul>
<?php pll_the_languages( array( 'show_flags' => 1,'show_names' => 0 ) ); ?>
</ul>
 
<!-- outputs a dropdown list of languages names -->
<?php pll_the_languages( array( 'dropdown' => 1 ) ); ?>

 

If the options are not enough, you can build your own custom language switcher using the ‘raw’ argument:

$translations = pll_the_languages( array( 'raw' => 1 ) );


The function will return an array of arrays, one array per language with the following entries:

 

[id] => language id
[slug] => language code used in urls
[name] => language name
[url] => url of the translation
[flag] => url of the flag
[current_lang] => true if this is the current language, false otherwise
[no_translation] => true if there is no available translation, false otherwise
The function is meant to display a language switcher. So it is not availaible on backend. If you need a function to get information about available languages, you should use pll_languages_list() instead.

 

to sume up : i sure have to do some tests and have to learn to use the referenced code.  - i do some tests and will try to digg deeper in that sort of problems. 

above all: i am so glad to be here at this great place of idea-sharing and knowledge-exchange.

plz - keep up this great place - it is one of the best places for all php- and internet things i ever have experienced

i love this forum!!!! 

 

regards dil_bert ;) )

 

 

Edited by dil_bert

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.