Display different content according to language in WordPress

 
Display different content according to language in WordPress

Translating a blog seems simple in principle, but it is not so simple. I'm not talking about the content even the template can resist.

Although there are plugins like Loco Translate and similar that make this task easier, and even templates that come ready to add the translation of almost any string, which can also be done with Polylang (although it is usually too short and you have to adding strings by hand), there are things that get complicated. Here I'm still struggling with the last string I have left to translate

It gets even more complicated if you want to display different content and/or layouts in specific parts of your site depending on the language.

These parts are usually the header, footer, sidebars and to a lesser extent in exact places between the content. There is a solution for all of these.

For both the header and the footer you can use Elementsif you use GeneratePress in its Pro version, and show a different layout for each language since the most popular translation plugins like WPML and Polylang are compatible with GeneratePress.

Mostrar contenido distinto según el idioma en WordPress

It is as simple as creating a hook or block of Elements in the desired language, give it the design you need and it is ready to be displayed in that language version.

Display different content according to language in WordPress

The example uses Elements from GeneratePress Pro and Polylang Free to create a footer in English.

This way you can create almost any part of your blog with a different design for each language, but what if you also want to show a different and unique content or design for each language?

Different sidebars for different languages

With Polylang, creating a sidebar for each language is very easy. You only have to create all the translated widgets for each language with the design you want and assign them the corresponding language.

Display different content according to language in WordPress

Display different content according to language in WordPress

Showing different content in the sidebar with conditions is complicated because with Elements we can add content before and after the sidebar, but not inside. Although with GeneratePress and/or GenerateBlocks you can even build a new sidebar from scratch for each language (a much more complete and recommended solution) it is more cumbersome and a lot more work

A quick, simple and equally effective alternative is to turn to Ad Inserter (the free version is enough) to create conditional sidebar widgets.

Here is an example of how to create a widget that only displays in the sidebar of the post.

Widgets with conditions

Although Ad Inserter is designed to manage and place ads almost anywhere, which it does perfectly, it can be used to display any other content.

Display different content according to language in WordPress

  1. You create a new block in Ad Inserter and give it a name that clearly describes what it is and where it will be displayed.
  2. Add the content to be displayed, which can be HTML for a form, text, an image with a link, a shortcode you have registered, etc.
  3. It is marked "Post" (as you will see you can assign other locations).
  4. In insert you should leave it at "Deactivated"because in this example we are going to use it as a widget.
  5. Check Widget and, if you want to use it in other places, you can also check " " and "PHP function" if you need to put it in the widget Shortcode and "PHP function" if you need to insert it somewhere in your template.
  6. Save the block.

When we go back to the widget administration area we will find that in the Ad Inserter widget the block we just created will appear. When you add it to your sidebar, this content will only be shown in posts. Now you can choose, for example, to display it only in the English version.

Display different content according to language in WordPress

Any different content according to language on any site

Now you can go one step further and play with the locations with a lot of possibilities and combinations to place anything almost anywhere else and have it displayed only according to the language.

Display different content according to language in WordPress

If we choose a location that Ad Inserter allows us and then click on"Lists" we can place that content in a lot of points with the possibility of adding white and black lists, conditions and exceptions at will.

In the example I have configured that this content is displayed on top of all the posts but only in the English version

Display different content according to language in WordPress

From here you can fine tune the insertion to appear before or after paragraph x, before, between or at the end of the content, etc and play with options such as excluding urls, ID's, categories, tags or insert only in a specific taxonomy, even according to the language of the visitor's browser using a simple shortcode. The combinations and positions are almost unlimited

Display different content according to language in WordPress

To learn more about placements and the use of lists see the documentation from Ad Inserter.


Suscríbete por email para recibir las viñetas y los artículos completos y sin publicidad

Artículos relacionados

Este blog se aloja en LucusHost

LucusHost, el mejor hosting