0
votes

Client just asked me to return different Facebook page links based on the language/region categories of Wordpress posts. For instance, if a user lands on a blog post in Portuguese, the FB icon in the header will dynamically change to "facebook.com/brandnamebrasil" instead of the default "facebook.com/brandname"

Here's my best attempt so far adding PHP meta in the theme header file. Any advice or pointers would be greatly appreciated:

<?php 
$theurl = $_SERVER['REQUEST_URI'];
$thecat = explode("/", $theurl);
$post_cat = $thecat[2];
if($post_cat == "category")
{$post_cat = $thecat[3];}
else
{$post_cat = $thecat[2];}

else if($post_cat == "latin-america-pt")
{$the_cat_url = "latin-america-pt/?lang=pt-br"; $mast_url="/wp-content/brasil_header.jpg"; $facebook_link = "https://www.facebook.com/brandnamebrasil";}

else
{$the_cat_url = $post_cat; $facebook_link="https://www.facebook.com/brandname/";} 

 ?>


<div style="position:relative; display: inline; width:1026px; height:128px; background: #fff; margin-top: 45px; padding: 10px 15px;"><a href="<?php echo site_url(); ?>/<?php echo $the_cat_url;?>"><img src="<?php echo site_url(); ?><?php echo $mast_url; ?>" border="0"/></a> 
    <a href="<?php echo $facebook_url;?>" style="display:block; float:right; padding:10px 17px 0px 10px;" target="_blank"><img src="/wp-content/uploads/2016/07/FB.jpg" class="social-icons"></a> 
    <a href="http://instagram.com/brandname" style="display:block; float:right; padding:10px 17px 0px 10px;" target="_blank"><img src="/wp-content/uploads/2016/07/instagram.jpg" class="social-icons"></a> 
    <a href="http://twitter.com/brandname" target="_blank" style="display:block; float:right; padding:10px 17px 0px 10px;" target="_blank"><img src="/wp-content/uploads/2016/07/twitter.jpg" class="social-icons"></a> 
    <a href="http://pinterest.com/brandname" style="display:block; float:right; padding:10px 17px 0px 10px;" target="_blank"><img src="/wp-content/uploads/2016/07/pinterest.jpg" class="social-icons"></a> 
</div>
1

1 Answers

0
votes

You are getting language information via urls. This could be dangerous if the url format changes in the future.

Are you using any plugins for different post languages? If yes most plugins have specific functions to return current post's languages. For example for polylang you use pll_current_language().

You can also use wordpress function get_locale() to get current language like this:

<?php if(get_locale() == 'zh_TW') : ?>
                    只需輸入您的姓名和電子郵件就可接收我們的最新文章:   
<?php else : ?> 
                    Simply enter your name and email to receive our latest posts:
<?php endif; ?>

Finally if you create your own wordpress categories to distinguish different post languages, you can use the_category() in the Loop to get your language category.

Hope this helps.