Adding conditional text to a PHP Shortcode Template

Question

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

in progress 0
Jimmyb 1 month 2021-08-19T15:45:17-05:00 0 Answer 0 views 0

Answer ( 1 )

    0
    2021-08-22T03:12:46-05:00

    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!";
    }
    
    ?>

Leave an answer

Browse
Browse