【问题标题】:How can I change button text, depends on site slug (PHP, Wordpress)如何更改按钮文本,取决于网站 slug(PHP、Wordpress)
【发布时间】:2019-11-22 12:26:31
【问题描述】:

首先,我想说我对PHP一无所知(这完全不是我的鞋)。我被要求在 Wordpress 主题中制作一些东西,这将翻译按钮中的文本,这取决于网站语言。差异仅在 slug 中可见,所以我假设,我必须以某种方式获取信息是 slug 包含 "en""de" 并将该信息传递给翻译功能。

问题是,我不知道从哪里开始。我将不胜感激。

<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 wordpress button translation


    【解决方案1】:

    将此代码放入functions.php文件

    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>');
    }
    

    如果您只想替换上面代码中的按钮文本很好,但如果您想在所有页面上设置使用基于语言的翻译插件。

    【讨论】:

    • 嗨,感谢@Dhruv 的帮助。我已经尝试了您的解决方案,但 wordpress 以错误响应。在行` `我收到语法错误,意外的“读取”(T_STRING),期待“,”或“)”
    • @Wiktor 它只是更新我的代码请尝试更新答案
    • 嗨@Dhruv 很抱歉延迟回复,但我还有很多其他事情要做。我更广泛地查看了这个案例,并意识到我没有与您分享代码的正确范围。经过几次错误,我终于找到了解决方案。这是我的适用于 WordPress 主题的代码行。非常感谢您的帮助!
    【解决方案2】:

    我认为它会完成这项工作。

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

    但是,如果您想翻译更多内容,则需要使用基于语言翻译的插件或创建您自己的插件。因为否则它很快就会变得难以维护

    【讨论】:

      【解决方案3】:
      <?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; ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-18
        • 2016-05-10
        • 2021-12-29
        • 2019-02-16
        • 2013-09-28
        • 1970-01-01
        • 2022-08-13
        • 1970-01-01
        相关资源
        最近更新 更多