php - Dynamic Banner Text based on Subdomain

admin2025-04-19  0

I'm working on a site where each agent has their own subdomain. Each subdomain directs to the content on the main page, there is no difference in content. The only difference requested is a banner that changes based on the subdomain used. For example: bob.example Would direct to example with a banner at the top that says "Welcome to Agent Bob's Portal!" I'm not very experienced with WordPress, but what I was aiming for was something like:

<?php $url = $_SERVER["REQUEST_URI"];
 if (strpos($url, "bob."))  {
           print "Welcome to Agent Bob's Portal!";
        }
} ?>

The problem is that I'm not quite sure where to put it. I've tried putting it in the page.php file, surrounded by a div that encapsulates the area I want the text in, but that doesn't seem to work. Is there something else you would recommend trying?

I'm working on a site where each agent has their own subdomain. Each subdomain directs to the content on the main page, there is no difference in content. The only difference requested is a banner that changes based on the subdomain used. For example: bob.example Would direct to example with a banner at the top that says "Welcome to Agent Bob's Portal!" I'm not very experienced with WordPress, but what I was aiming for was something like:

<?php $url = $_SERVER["REQUEST_URI"];
 if (strpos($url, "bob."))  {
           print "Welcome to Agent Bob's Portal!";
        }
} ?>

The problem is that I'm not quite sure where to put it. I've tried putting it in the page.php file, surrounded by a div that encapsulates the area I want the text in, but that doesn't seem to work. Is there something else you would recommend trying?

Share Improve this question edited Oct 22, 2019 at 5:57 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Oct 22, 2019 at 2:27 LisaLisa 111 bronze badge 1
  • I would take a look at a better function to get the subdomain - primitivetype/snippets/php_get_subdomain_from_host.php – Кристиян Кацаров Commented Oct 22, 2019 at 7:15
Add a comment  | 

1 Answer 1

Reset to default 0

You can put you code in functions.php where you define the string, for example:

<?php   
function get_banner_text() {
  $url = $_SERVER["REQUEST_URI"];
  if (strpos($url, "bob."))  {
    $banner_text = "Welcome to Agent Bob's Portal!";
  } else {
    $banner_text = "Default text here";
  }
  return $banner_text;
}
?>

Afterwards for example in header.php of your theme you could have like:

<div class="banner"><?php echo get_banner_text(); ?></div>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745062625a282791.html

最新回复(0)