1

First of all, I wanted to say that I know nothing about PHP (it is totally not my pair of shoes). I was asked to make something in Wordpress theme, that will translate text in the button, depends on site language. The difference is visible only in slug so I assume, that I have to somehow get info is slug contains "en" or "de" and pass that info to translate function.

Problem is, that I don't know where to start. I will appreciate any kind of help.

<div class="read-more clearfix">
        <a class="button post-button" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php esc_html_e('Read more', 'astrid'); ?></a>
</div>

3 Answers 3

1

Put this code in functions.php file

if(strpos($_SERVER['REQUEST_URI'], 'de') !== false ){
        //If your slug is de
    _e('<div class="read-more clearfix">
        <a class="button post-button" href="'.the_permalink().'" title="'. the_title().'">'.esc_html("Read more", "astrid").'</a>
        </div>');
}
else{
        //If your slug is en
    _e('<div class="read-more clearfix">
        <a class="button post-button" href="'.the_permalink().'" title="'.the_title().'">'.esc_html("Read more", "astrid").'</a>
        </div>');
}

If you want to only replace a button text above code are good but if you want to set on all page use language based translation plugin.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks @Dhruv for help. I've tried your solution and wordpress responded with an error. In line ` <a class="button post-button" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php esc_html_e('Read more', 'astrid'); ?></a> ` I received syntax error, unexpected 'Read' (T_STRING), expecting ',' or ')'
@Wiktor it just update my code please try with updated answer
Hi @Dhruv sorry for the delay with a reply but I had a lot of other things to do. I looked at this case a little bit wider and realize that I didn't share with you the proper scope of the code. After a couple of mistakes, I finally found a solution. Here are my lines of code that works with the WordPress theme. Thanks a lot for the help!
1

I think it'll do the job.

function the_title(){
  $language = $_GET["slug"];
  $buttonText = "";

  switch($language){
    case "fr":
      $buttonText = "Le texte de mon button";
      break
    case "de":
      $buttonText = "Mein Schaltflächentext";
      break

// etc 

    case default:
      $buttonText = "My button text";
      break 
  }
  return $buttonText;
}

But if you want to translate more things you'll need to use language translation based plugin or create yours. Because otherwise it will quickly become difficult to maintain

Comments

0
<?php elseif(strpos($_SERVER['REQUEST_URI'], 'en') !== false ): ?>

<div class="entry-summary">
<?php the_excerpt(); ?>
</div>

<div class="read-more clearfix">
<a class="button post-button" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php esc_html_e('Read more', 'astrid'); ?></a>
</div>

<?php else : ?>

<div class="entry-summary">
<?php the_excerpt(); ?>
</div>

<div class="read-more clearfix">
<a class="button post-button" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php esc_html_e('Czytaj więcej', 'astrid'); ?></a>
</div>

<?php endif; ?>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.