Contents
On multilingual websites, there are often use cases where certain elements should be displayed or hidden depending on the language. Depending on the plugin, there are different ways to achieve this. This article deals explicitly with the implementation of a TranslatePress condition for Breakdance.
Display depending on the URL
One way to achieve this with the on-board tools of Breakdance is to use conditions to retrieve the URL and check whether a certain language code exists in the string. For example, you could check whether the string "/en/" is in the URL and show or hide the element depending on this.
Use TranslatePress shortcodes
TranslatePress also offers shortcodes that can be used for this purpose. To do this, insert all content that is to be shown/hidden into the shortcode and adjust the corresponding parameters.
[trp_language language="fi"] English content only [/trp_language]
Custom TranslatePress Breakdance Condition
The most elegant way to achieve your goal is to add a custom condition for TranslatePress to Breakdance. To do this, you can add the following PHP code to your installation.
After you have inserted this code, the custom condition "TranslatePress" appears in the Breakdance Conditions. All languages that you have set up are displayed here. As with the other Breakdance conditions, you can apply the condition to elements here. The corresponding elements are then displayed or hidden depending on the selected language.
<?php
add_action("breakdance_register_template_types_and_conditions", function () {
if (function_exists("trp_get_languages")) {
$lang_list_full = trp_get_languages();
$lang_list = array();
foreach($lang_list_full as $key => $value) {
$lang_list[] = $key;
}
\Breakdance\ConditionsAPI\register([
"supports" => ["element_display", "templating"],
"slug" => "TranslatePress4bd-condition", // MUST BE UNIQUE
"label" => "Language",
"category" => "TranslatePress",
"operands" => ["equals", "not equals"],
"values" => function () use ($lang_list) {
return [
[
"label" => "Language",
"items" => array_map(function ($lang) {
return [
"text" => $lang,
"value" => $lang,
];
}, $lang_list),
],
];
},
"allowMultiselect" => false,
"callback" => function (string $operand, $value) {
// Get the current language from TranslatePress
$current_lang = get_locale();
if ($operand === "equals") {
return $current_lang === $value;
}
if ($operand === "not equals") {
return $current_lang !== $value;
}
return false;
},
]);
}
});