首页 > 解决方案 > 如何为 WordPress 多个子域设置 if-else 条件?

问题描述

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

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

?>

<?php echo $language; ?>

我在主域下有 4 个子域。

喜欢:

  1. 示例.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 条件相同的子域打印变量。

标签: phpwordpresssubdomain

解决方案


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

}
}

推荐阅读