Fixing the page title problem in multilingual sites

یکی از چالشهای رایج در ساخت سایتهای چندزبانه با وردپرس اینه که بتونیم عنوان صفحات (Title Tag) رو برای هر زبان بهصورت جداگانه تنظیم کنیم. اگر از افزونه TranslatePress نسخه رایگان استفاده کرده باشید، احتمالاً متوجه شدهاید که امکان ترجمه مستقیم تایتل صفحات وجود نداره. همین موضوع باعث میشه مثلاً وقتی سایت فارسی رو انگلیسی میکنید، عنوان صفحه همچنان فارسی باقی بمونه. از طرف دیگه، بیشتر مدیران سایتها برای بهینهسازی سئو از Yoast SEO استفاده میکنن. این افزونه کنترل تایتل صفحات رو در دست میگیره، اما بهصورت پیشفرض راهحلی برای چندزبانه کردن تایتلها در نسخه رایگان TranslatePress نداره.
Solution: Use coding in functions.php
To solve this problem, we can manually specify the English titles for each page using Yoast SEO filters and a little coding in the functions.php file of the template.
- Check the active language of the site
The first step is to recognize the language that the user has chosen. In WordPress, we can get_locale() Take the active language. For example:
if ( get_locale() === 'en_US' ) {
// اینجا یعنی زبان روی انگلیسی تنظیم شده
}
- Changing the title of the pages according to the conditions
Now we can bet for any page or section of the site. Simple example:
if ( get_locale() === 'en_US' ) {
// صفحه اصلی
if ( is_front_page() ) {
return "Karin Company";
}
// بلاگ
if ( is_home() ) {
return "Karin | Blog";
}
// نوشتههای تکی
if ( is_single() && 'post' === get_post_type() ) {
return "Karin | Blog | " . get_the_title();
}
}
Important points
- For specific pages, you can use Pages ID or Page slug use
- For the blog page, you must meet the conditions
is_home()use, because WordPress treats it as a "posts" page, not a normal page. - This method is only applied to the English language (en_US). If you want to add other languages, you can create new terms for
fa_IROr write any other locale.
summary
With this method, you can have full control over the title of multilingual pages without the need for the paid version of TranslatePress or external plugins, and make sure that the titles are displayed correctly for each language.
Finally,
If you have encountered such technical problems on your WordPress site and are looking for a team that will find the right and basic solution, we do exactly the same thing in our work: from design to development and SEO, so that your site works properly and is seen in Google results.
