【问题标题】:How to make an if-else condition for WordPress multiple subdomains?如何为 WordPress 多个子域设置 if-else 条件?
【发布时间】:2021-08-30 19:20:44
【问题描述】:

此代码在我的 wordpress 网站页面上运行良好。

<?php 
if ( is_page('pt') ) {
  $language = 'pt';
} 
  else if( is_page('es') ) {
    $language = 'es';
  }
     else {
  $language = 'en';
     }

?>

<?php echo $language; ?>

我在主域下有 4 个子域。

喜欢:

  1. example.domain.com
  2. example1.domain.com
  3. example2.domain.com

我想对子域应用相同的编码方法。

例如:

<?php 
if ( is subdomain example.domain.com ) {
  $language = 'pt';
} 
  else if( is subdomain example1.domain.com ) {
    $language = 'es';
  }

  else if( is subdomain example2.domain.com ) {
    $language = 'ar';
  }
    
     else {
  $language = 'en';
     }

?>

如何打印与 WordPress is_page 条件相同的子域的变量。

【问题讨论】:

    标签: php wordpress subdomain


    【解决方案1】:
    <?php
    
    if(isset($_SERVER['HTTPS'])){
    
    $domain = $_SERVER['HTTPS_HOST'];
    
    switch($domain){
    
    case 'example.domain.com': Your code...;
    
    break;
    
    case 'example1.domain.com': Your code...;
    
    break;
    
    case 'example2.domain.com': Your code...;
    
    break;
    
    default: Your code...;
    
    }
    }
    

    【讨论】:

    • 我从未在任何地方见过$_SERVER['HTTPS_HOST']。可能是填充此条目的某些特定服务器设置(?),但通常您只会使用 $_SERVER['HTTP_HOST'],无论请求是使用 HTTP 还是 HTTPS 发出的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 2012-12-22
    • 1970-01-01
    • 2020-05-31
    • 2019-07-01
    • 2021-03-07
    • 2015-06-23
    相关资源
    最近更新 更多