Blog page description with two different languages

I want to add to my blog page (domain/blog) description text.

My site has two languages and I use Polylang free version plugin. It means I have 2 blog pages one for language-1 (url: domain/blog) and one for language-2 (url: domain/en/blog-en). I know how to add (generally) this description part to my blog page (I added blog title too):

I insert into home.php (in child theme) under

<main id="main" class="post-wrap" role="main"> part

this

<h1 style="text-align: center;padding-bottom: 40px;color: #111111;">Blog</h1>
<p style="text-align: center;padding-bottom: 40px;">description text</p>

But this way I got this description text by both languages. (polylang free version gives no support for making two different language version in this case)

So do you have any idea how could I add two different blog page descriptions for this two “different” blog pages?

Hello there,

The solution for this might be wrapping the code with current language check. And display the translated text for the respective (current active) language. This could be accomplished with the Polylang Conditional extension.

Once installed and enabled, you can use is_language( $lang ) check to detect current language.

Regards,
Kharis

Deer Kharis,

thank you for your answer. Could you show me an example code with my code part eg for english language version?

My code part:
<h1 style="text-align: center;padding-bottom: 40px;color: #111111;">Blog</h1>
<p style="text-align: center;padding-bottom: 40px;">description text</p>

Polylang Conditional code part example: [is_language lang=“en”]CONTENT[/is_language]

How to combinate it with my codes above?

Hello there,

Try this code:

    $blog_heading = 'Blog';
    $blog_desc = 'Description text';

    if( is_language('en') ) {
    	$blog_heading = 'Blog in English version';
    	$blog_desc = 'Description text in English version';
    }

    echo '<h1 style="text-align: center;padding-bottom: 40px;color: #111111;">'.$blog_heading.'</h1>';
    echo '<p style="text-align: center;padding-bottom: 40px;">'.$blog_desc.'</p>';

Before updating ensure the entire code doesn’t contain any PHP error. You could copy/paste all the code home.php and check with this tool: https://phpcodechecker.com/.

Regards,
Kharis

Deer Kharis,

thx for your help:

  • I implemented the code in home.php (in my case en version is en-US)
  • I checked the home.php with phpcodechecker: results = No issues found.
  • I updated the home.php
  • (of course Polylang Conditional plugin is activated)
  • but it doesn’t work i get this on the page (see on the attached picture)

any other idea?

Hello there,

Please copy/paste your home.php code to https://gist.github.com/ and share the link into it in your next reply. So I can check and reproduce it on my test site.

Regards,
Kharis

Dear Kharis,

Here is my home.php

Hereby you can see only the “en” version but know I insertet it only for checking and I want to use this for 2 language. Please use two language samples (1 for primary languge and 1 for english)

Try replacing this code block with:

<?php

$blog_heading = 'Blog';
$blog_desc = 'Description text';

if( is_language('en') ) {
  $blog_heading = 'Blog in English version';
  $blog_desc = 'Description text in English version';
}

echo '<h1 style="text-align: center;padding-bottom: 40px;color: #111111;">'.$blog_heading.'</h1>';
echo '<p style="text-align: center;padding-bottom: 40px;">'.$blog_desc.'</p>';

?>

Regards,
Kharis

Perfect, Thank you very much Kharis :slight_smile:

You’re welcome!

Please let us know in a new topic if you have any further questions, or if we can provide you with any other assistance.

Regards,
Kharis