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.

Read More

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?

Related posts

Leave a Reply

3 comments

  1. 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.

  2. 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
    }