0

So I'm using the following code:

<?php
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host == 'domain.com/portfolio/travel/') 
{ ?>
<style>
@import url('<?php echo get_template_directory_uri(); ?>/vertical-portfolio.css');
</style>
<?php } ?>

Based on what I found on PHP if URL equals this then perform action

Whilst it works at it's core, it's not /travel itself I want the code imported into, but in actual fact sub's of that (ie travel/bermuda, travel/canada, etc etc)

What variable would I include to go about doing this?

Community
  • 1
  • 1
  • Your question seems confusing to me. – Ali Gajani Dec 31 '13 at 06:16
  • Essentially, I want to include a file into a template if that file is WITHIN a certain directory. So domain.com/travel wouldn't call the file, but domain.com/travel/COUNTRY would - where COUNTRY is a variable. TLDR: if file is in folder x, then include. – Alex Masters Dec 31 '13 at 06:17
  • may be `strpos` will work in your case like `strpos($host, 'travel')` – Harish Singh Dec 31 '13 at 06:18

1 Answers1

1

If you want to check whether the URL begins with that path, use:

if (strpos($host, 'domain.com/portfolio/travel/') === 0)

Make sure you use the exact comparison ===. If the string isn't found, strpos() returns false, which will match 0 if you use ==.

To look only for subdirectories, you can use this:

if (preg_match('#^domain.com/portfolio/travel/.*/#', $host))

Go to regular-expressions.info to learn more about regular exressions.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Will that also apply to portfolio/travel itself? I only want the files WITHIN travel to be affected. I thought there would be a little variable of sorts like if($host == 'domain.com/portfolio/travel/$variable') – Alex Masters Dec 31 '13 at 06:20
  • `portfolio/travel` is a directory, not a script. – Barmar Dec 31 '13 at 06:22
  • I know. I just thought I'd seen a dollar sign + letter to indicate things within a directory once. – Alex Masters Dec 31 '13 at 06:24
  • That would be how you _construct_ a pathname that inserts the variable's value into the string. It has nothing to do with pattern matching. – Barmar Dec 31 '13 at 06:25
  • Ah. Adjusted code with yours. /travel is affected in addition to the pages therein. Is there any way of excluding the travel tier itself? I suppose (based on how WP constructs things) it'd need only apply to sub-directories rather than pages, assuming /travel calls /travel/indexfileofsomesort.extension – Alex Masters Dec 31 '13 at 06:27
  • That's because `/traval` is actually something like `/travel/index.php`, which is in the directory just like all the other scripts there. – Barmar Dec 31 '13 at 06:29
  • You could use a regular expression. – Barmar Dec 31 '13 at 06:30
  • Fabulous at CSS. Completely inept at PHP. Laymans would be greatly appreciated. – Alex Masters Dec 31 '13 at 06:34
  • Tried using a wildcard (?) -> /travel/* but that doesn't seem to have done anything aside from not work. – Alex Masters Dec 31 '13 at 06:59
  • `==` just does simple equality comparison, nothing special related to filenames. – Barmar Dec 31 '13 at 07:19
  • Does the regular expression solution I added to my answer do what you want? – Barmar Dec 31 '13 at 07:20