Wiktor
Wiktor

Reputation: 13

How can I change button text, depends on site slug (PHP, Wordpress)

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>

Upvotes: 1

Views: 1084

Answers (3)

Wiktor
Wiktor

Reputation: 13

<?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; ?>

Upvotes: 0

Dhruv
Dhruv

Reputation: 612

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.

Upvotes: 1

Ichinator
Ichinator

Reputation: 357

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

Upvotes: 1

Related Questions