** page.php **
** content-page.php **

WP URL Accessing

Testing the page url for specific strings


WordPress PHP – This is useful for finding a specific word in the path url of a page, this allows a section/folder of pages to be targeted with a unique css styling or the like.

This example it was used to find a website’s section from the url. This was “portfolio” and the task to add an extra class to a div around the image element. The following PHP is placed in the PHP template file (possibly page.php) at the relevant point.

Sourced PHP code to preform this is:

<?php $url = ‘http://’ . $_SERVER[‘SERVER_NAME’] . $_SERVER[‘REQUEST_URI’];
if (strpos($url,’portfolio’) !== false) {
echo ‘<div class="fp-featuredimgbox fp-port">’;
} else {
echo ‘<div class="fp-featuredimgbox">’;
} ?>

First line interrogates the page’s url and then checks for the specified value of ‘portfolio’.
Then the if statement echoes either of two different terms.

Done.
Fivepilchard Team fp_logo_green

Updated Jan 2017


Contact us for more information