Conditional Content Publication
Content can be published to a web page depending on whether or not the web page is in a specific directory.
As an example of use, the navigation area of web pages in the /widget/ directory need a link to each page in the /widget/ directory. But not links to each page in other directories.
As another example of use, let's suppose you want to publish a "See Our New Widget Pages" link on every web page – except the /widget/ directory (because they're already looking at the widget pages).
The PHP code to accomplish it is a two-part snippet. The content to be published is between the two parts. There may be many snippet/content sets to address many different directories.
All the snippet/content sets can be contained in one central file to be imported into individual web pages. Changes to the central file are relatively easy and quick.
You'll find 4 things in this article.
-
The code to import the central PHP file into web pages
-
The code and an example of publishing certain content only if the web page is in a certain directory.
-
The code and an example of publishing certain content only if the web page is not in a certain directory.
-
An example central PHP file.
Importing the Central PHP File
I will assume the central file is named central.php and that it is located in the document root directory. (The document root directory is where the domain's main/index page is at.)
This is the the code to import the central file into a web page:
<?php include($_SERVER["DOCUMENT_ROOT"]."/central.php"); ?>
If your external file is not named central.php or if it is not located in the document root directory, change the above accordingly. For example, if it is located in the "external" subdirectory and named "nav.php", then the PHP code to import it is:
<?php include($_SERVER["DOCUMENT_ROOT"]."/external/nav.php"); ?>
Publishing Only When in a Certain Directory
This will print certain content only when the web page is in the /widget/ directory.
<?php if( strpos($_SERVER['PHP_SELF'],"/widget/") !== false ): ?> Content to publish. <?php endif; ?>
The first and last lines are the PHP two-part snippet. Everything between those lines is the content. The content can be anything that can be published on a web page.
The content will be published if the web page being loaded is in the /widget/ directory. Otherwise, the content will not be published on that web page.
To change the directory name, change the word "widget" in the first line to the new directory name.
Here is an example to publish certain links only in the /widget/ directory.
<?php if( strpos($_SERVER['PHP_SELF'],"/widget/") !== false ): ?> <h1>Widget Directory</h1> <p><a href="one.php">One</a></p> <p><a href="two.php">Two</a></p> <p><a href="three.php">Three</a></p> <?php endif; ?>
Publishing Only When Not in a Certain Directory
This will print certain content only when the web page is not in the /widget/ directory.
<?php if( strpos($_SERVER['PHP_SELF'],"/widget/") === false ): ?> Content to publish. <?php endif; ?>
Everything between the first and last lines will be published if the web page being loaded is not in the /widget/ directory. To change the directory name, change the word "widget" in the first line to the new directory name.
This example with publish a "See Our New Widget Pages" link to web pages in any directories except the /widget/ directory.
<?php if( strpos($_SERVER['PHP_SELF'],"/widget/") === false ): ?> <h3> <a href="/widget/index.php"> <span style="color:white; background-color:blue"> See Our New Widget Pages </span> </a> </h3> <?php endif; ?>
An Example Central PHP File
The following example central PHP file specifies three directories for specific content: /widget/, /smidget/ and /mo/. And it also specifies content to be printed to any web pages except those in the /widget/ directory.
<?php if( strpos($_SERVER['PHP_SELF'],"/widget/") !== false ): ?> <h1> I print in the "widget" directory. </h1> <?php endif; ?> <?php if( strpos($_SERVER['PHP_SELF'],"/smidget/") !== false ): ?> <h1> I print in the "smidget" directory. </h1> <?php endif; ?> <?php if( strpos($_SERVER['PHP_SELF'],"/mo/") !== false ): ?> <h1> I print in the "mo" directory. </h1> <?php endif; ?> <?php if( strpos($_SERVER['PHP_SELF'],"/widget/") === false ): ?> <h3> <a href="/widget/index.php"> See Our New Widget Pages </a> </h3> <?php endif; ?>
The three named directories will print the content relevant to the directories. All directories except /widget/ will print the See Our New Widget Pages link.
The two-part PHP snippet does a lot of work. It needs to be imported into a PHP web page, of course, in order to run the PHP code. PHP web pages usually have a .php file name extension.
Import the central PHP file into all web pages to be affected. When a browser requests a page, the PHP determines what is and is not published.
Will Bontrager