Skip Navigation

[Resolved] Print post language (WPML) in a view

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to get the current post language.
Solution:
This can be done with the custom shortcode that I made for this.

// Add Shortcode
function current_language( $atts ) {
  
    // Attributes
    $atts = shortcode_atts(
        array(
            'id' => '',
            'part' => '',
        ),
        $atts
    );
    $language = wpml_get_language_information($atts['id']);
    return $language[$atts['part']];
  
}
add_shortcode( 'current_language', 'current_language' );

How this works is that it gives you the shortcode [current_language id='[wpv-post-id]' part='display_name'] this should give you the correct information.

Also you can add the code for the shortcode in the toolset custom code section under Toolset -> Settings -> Custom Code.

This support ticket is created 5 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 6 replies, has 2 voices.

Last updated by AndreasP4791 5 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#1243129

I have created a custom view and I want now to print post language with flag or (language) text.

I have also bought WPML, so my site is multilingual with this plugin.

How I will find and print the language of the post?

Thank you!

#1243203

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Andreas,

I managed to whip up a quick shortcode that should be able to help you.

// Add Shortcode
function current_language( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'id' => '',
			'part' => '',
		),
		$atts
	);

	$language = wpml_get_language_information($atts['id']);
	
	return $language->atts['part'];

}
add_shortcode( 'current_language', 'current_language' );

How this works is that it gives you the shortcode [current_language id='[wpv-post-id]' part='display_name'] this should give you the correct information.

Also you can add the code for the shortcode in the toolset custom code section under Toolset -> Settings -> Custom Code.

Thanks,
Shane

#1243211

Hello Shane,

Thank you for your reply. I follow your instructions but no result.

I pat var_dump($atts); and the output is the following:

array(2) { ["id"]=> string(5) "10362" ["part"]=> string(12) "display_name" } array(2) { ["id"]=> string(5) "10324" ["part"]=> string(12) "display_name" } array(2) { ["id"]=> string(5) "10291" ["part"]=> string(12) "display_name" } array(2) { ["id"]=> string(5) "10094" ["part"]=> string(12) "display_name" } array(2) { ["id"]=> string(5) "10091" ["part"]=> string(12) "display_name" } array(2) { ["id"]=> string(5) "10087" ["part"]=> string(12) "display_name" } array(2) { ["id"]=> string(4) "2343" ["part"]=> string(12) "display_name" } array(2) { ["id"]=> string(4) "2342" ["part"]=> string(12) "display_name" } array(2) { ["id"]=> string(4) "2339" ["part"]=> string(12) "display_name" } array(2) { ["id"]=> string(4) "2338" ["part"]=> string(12) "display_name" } 

.

I also put var_dump($language); and the output is this:

array(6) { ["language_code"]=> string(2) "en" ["locale"]=> string(5) "en_US" ["text_direction"]=> bool(false) ["display_name"]=> string(7) "English" ["native_name"]=> string(7) "English" ["different_language"]=> bool(false) } array(6) { ["language_code"]=> string(2) "en" ["locale"]=> string(5) "en_US" ["text_direction"]=> bool(false) ["display_name"]=> string(7) "English" ["native_name"]=> string(7) "English" ["different_language"]=> bool(false) } array(6) { ["language_code"]=> string(2) "en" ["locale"]=> string(5) "en_US" ["text_direction"]=> bool(false) ["display_name"]=> string(7) "English" ["native_name"]=> string(7) "English" ["different_language"]=> bool(false) } array(6) { ["language_code"]=> string(2) "en" ["locale"]=> string(5) "en_US" ["text_direction"]=> bool(false) ["display_name"]=> string(7) "English" ["native_name"]=> string(7) "English" ["different_language"]=> bool(false) }

.

Could you help me more please? How I can also output the flag icon?

Thank you indeed!

Andreas

#1243227

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Andreas,

I made an update to the code.

Try this.


// Add Shortcode
function current_language( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'id' => '',
            'part' => '',
        ),
        $atts
    );
    $language = wpml_get_language_information($atts['id']);
    return $language[$atts['part']];
 
}
add_shortcode( 'current_language', 'current_language' );
#1243230

Thank you!

This works now!

How can I print the language flag?

#1243232

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Andreas,

For this particular question I would recommend getting in touch with the WPML team since they should have a better knowledge of the right function to use to get this.

Thanks,
Shane

#1243255

My issue is not totally resolved - I have to access WPML team.. Thank you!