Isaac Lubow
Isaac Lubow

Reputation: 3573

How to load a block of code conditionally using PHP?

I have a single installation of Wordpress that is powering two domain names. To put it very simply, Site A has four static pages, and Site B has four static pages, but they both share a common blog-posts page.

Site A should have a navbar at the top that points to the other Site A pages and Site B should have a navbar that points to the other Site B pages.

Since they (necessarily) share a common WordPress theme, in the header.php file I would like to put a PHP if statement for a block of code that is the Site A nav and another for the Site B nav. But I'm not sure what condition to actually check for. Is there some way I can cause some pages to identify themselves to PHP as belonging to Site A, and others to Site B?

Upvotes: 2

Views: 173

Answers (3)

Michael Irigoyen
Michael Irigoyen

Reputation: 22947

You could set variables in both sites before the header call, then use an if statement to show the right navigation.

Site A

<?
$site = 'A';
require('header.php');
?>

header.php

<?
if($site == 'A') {
  //Site A nav
} else {
  //Site B nav
}
?>

However, if your two sites have different domain names, you could look at the $_SERVER global and key off of that directly in your header.php instead.

Upvotes: 1

profitphp
profitphp

Reputation: 8354

if ($_SERVER['SERVER_NAME']=="www.whatever.com") {
//show site a header
} 

Upvotes: 0

amosrivera
amosrivera

Reputation: 26514

If this two websites have different domain name you can validate by doing:

if($_SERVER['HTTP_HOST']=="sitea.com"){
   //code for site a
} else {
   //code for site b
}

Upvotes: 0

Related Questions