如何使用 PHP 有条件地加载代码块?
我安装了一个 WordPress,它为两个域名提供支持。简而言之,站点 A 有四个静态页面,站点 B 有四个静态页面,但它们都共享一个公共博客文章页面。
站点 A 的顶部应有一个导航栏,指向站点 A 的其他页面,站点 B 的顶部应有一个导航栏,指向站点 B 的其他页面。
由于它们(必然)共享一个共同的 WordPress 主题,因此在 header.php
文件中,我想为站点 A 的代码块添加 PHP if
语句导航,另一个用于站点 B 导航。但我不确定实际检查什么条件。有什么方法可以让 PHP 将某些页面标识为属于站点 A,而其他页面则标识为属于站点 B?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在标头调用之前在两个站点中设置变量,然后使用 if 语句来显示正确的导航。
站点 A
header.php
但是,如果您的两个站点具有不同的域名,您可以查看
$_SERVER
全局并关闭它直接在您的header.php
中。You could set variables in both sites before the header call, then use an if statement to show the right navigation.
Site A
header.php
However, if your two sites have different domain names, you could look at the
$_SERVER
global and key off of that directly in yourheader.php
instead.如果这两个网站具有不同的域名,您可以通过执行以下操作进行验证:
If this two websites have different domain name you can validate by doing: