0

Right off the bat: I'm not a coder, been doing websites for a short time and this is my first post here. I never have to code, I'm sure this is a very elementary code for someone experienced so just seeing if someone could point me in the right direction.

I have a PHP template for a shortcode from a portal plugin (WP-Client) on a Wordpress site that I need to add a "conditional text" to. (I already asked them for help and they couldn't help me).

Explanation:

When the shortcode is placed on a page, it displays a message center with Inbox, Sent, Trash, etc. in English. My website is multi-lingual so I need to be able to change those labels on certain pages only. My thought on this was to insert a code into the template that says to put "Inbox" if page ID/name is "Example 1" and put "Bandeja de entrada" if the page ID/name is "Example 2".

This is the template I'm dealing with:


<div class="wpc_private_messages_shortcode">

    <div class="wpc_msg_nav_wrapper">

        <input type="button" class="wpc_msg_new_message_button wpc_button" value="<?php _e( 'New', WPC_CLIENT_TEXT_DOMAIN ); ?>" title="<?php _e( 'New Message', WPC_CLIENT_TEXT_DOMAIN ); ?>"/>

        <div class="wpc_msg_nav_list_wrapper">

            <div class="wpc_msg_nav_list_collapsed"></div>

            <ul class="wpc_msg_nav_list">
                <li class="wpc_nav_button inbox" data-list="inbox"><?php _e( 'Inbox', WPC_CLIENT_TEXT_DOMAIN ); ?></li>
                <li class="wpc_nav_button sent" data-list="sent"><?php _e( 'Sent', WPC_CLIENT_TEXT_DOMAIN ); ?></li>
                <li class="wpc_nav_button archive" data-list="archive"><?php _e( 'Archive', WPC_CLIENT_TEXT_DOMAIN ); ?></li>
                <li class="wpc_nav_button trash" data-list="trash"><?php _e( 'Trash', WPC_CLIENT_TEXT_DOMAIN ); ?></li>
            </ul>
        </div>


    </div>

As you can see, there are the 5 labels: New, Inbox, Sent, Archive, and Trash. These are the ones I need to be able to change to another language based on the page number/name.

I've gone through probably 10 different ways of writing it but I'm not quite getting there.

I was thinking something along the lines of this (but using page ID/name):

<?php
$t = date("H");

if ($t < "10") {
  echo "Have a good morning!";
} elseif ($t < "20") {
  echo "Have a good day!";
} else {
  echo "Have a good night!";
}
?>

Would appreciate any help on this.

Thanks,

James

1 Answer 1

1

The strings use in your plugin are using translatable strings ie. _e( 'Inbox', WPC_CLIENT_TEXT_DOMAIN )

It would probably be a better idea to use a translation plugin such as WPML or Polylang as opposed to trying to do this conditionally within the template files.

However, if you prefer to do this within the template file instead, you should maybe look at using the PHP Switch statement (https://www.w3schools.com/php/php_switch.asp), eg.

<?php

$pageid = get_the_ID();

switch ($pageid) {
  case 123:
    echo "Your favorite color is red!";
    break;
  case 456:
    echo "Your favorite color is blue!";
    break;
  case 789:
    echo "Your favorite color is green!";
    break;
  default:
    echo "Your favorite color is neither red, blue, nor green!";
}

?>
2
  • Thank you Mat! I used your code and it worked perfectly! I actually use Polylang on my site. I am trying to figure out how it works with the translation string you mentioned. Could you point me in the right direction on how do use Polylang to translate that string? I'm looking into it right now but if you know what I should be looking for that would be much appreciated! Thanks again for your help! James
    – Jimmyb
    Commented Aug 21, 2021 at 18:00
  • Polylang should have a section for string translation, its been ages since I used that plugin, but the string you want to translate should be there, in a group with all translatable strings that are using the same textdomain.
    – Laloptk
    Commented Aug 23, 2021 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.