Peter Knight

If URL or path contains

Recommended Posts

if ($page->path == "/blog/posts/"){
echo "<script type='text/javascript' src='//'></script>";

I'm trying to output a script on my blog posts only. They're all children of /blog/posts/ so I thought the above might work.

That seems to work only when the page loaded is exactly /blog/posts/

How can I tweak this to work on any page containing /blog/posts/ in it's URL / path and not just  /blog/posts/ ? 

I've been to the API and tried a few  variations using the following

if ($page->path *= "/blog/posts/")

but I think they're more selectors for fields?

Share this post

Link to post
Share on other sites

You can use strpos() function

if (strpos($page->path, "/blog/posts/") === 0){
	echo "...";


Share this post

Link to post
Share on other sites
if($page->matches("has_parent=/blog/posts/")) {


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Peter Knight
      I was trying to fix an error earlier which was preventing a series of if statements from working.
      Basically I have a button called "Product Drawing" which needs localising depending on the current language session.
      Eventually got it working - I had a bracket in the wrong place.
      Curious as to why both these work though.
      On the sample below. each echo is surrounded by curly braces.
      <?php if ($page->files->count()) foreach($page->files->findTag('drawing') as $file) { if($user->language->name == 'default') {echo "<a href='$file->url' class='uk-button uk-button-primary'>Product drawing</a>";} if($user->language->name == 'french') {echo "<a href='$file->url' class='uk-button uk-button-primary'>Le plan du produit</a>";} if($user->language->name == 'german') {echo "<a href='$file->url' class='uk-button uk-button-primary'>Produktzeichnung</a>";} if($user->language->name == 'spanish') {echo "<a href='$file->url' class='uk-button uk-button-primary'>El dibujo del producto</a>";} if($user->language->name == 'italian') {echo "<a href='$file->url' class='uk-button uk-button-primary'>Il disegno del prodotto</a>";} } ?>  
      On the sample below, there are no curly braces around the echo
      <?php if ($page->files->count()) foreach($page->files->findTag('breakingreport') as $file) { if($user->language->name == 'default') echo "<a href='$file->url' class='uk-button uk-button-primary'>Breaking strain report</a>"; if($user->language->name == 'french') echo "<a href='$file->url' class='uk-button uk-button-primary'>Le test de rupture</a>"; if($user->language->name == 'german') echo "<a href='$file->url' class='uk-button uk-button-primary'>Bruchfestigkeit bericht</a>"; if($user->language->name == 'spanish') echo "<a href='$file->url' class='uk-button uk-button-primary'>Informe resistencia</a>"; if($user->language->name == 'italian') echo "<a href='$file->url' class='uk-button uk-button-primary'>La rottura rapporto ceppo</a>"; } ?>  
      Should the second piece work?
    • By ngrmm
      i have a repeater with 2 fields
      – image
      – text
      if there are images uploaded then the output should be only the image field.
      if no images uploaded, then only the text-field should be shown
      right now, if the image-field is empty and the text-field is filled. i'm getting two boxes. and the first one is empty.
      <?php foreach($page->repeater as $box) { if($box->images) { ?> <div class="box images"> <?php foreach($box->images as $image) { echo "<img src='$image->url'/>"; } ?> </div> <?php } else { }   if($box->text) { ?> <div class="box text"><?php echo $box->text; ?></div> <?php } } ?>
    • By Deyan
      this is probably very easy to solve but I just can get the right output. This is the code:
      <?php //$tender_open is checkbox field; $tenders = $pages->find("template=tender, sort=-created, tender_open=$open"); ?> <h3>Tenders</h3> <?php if($open == 0) { echo '<p>There are no tenders available !</p>'; } else { ?> <ul class="no-style"> <?php foreach ($tenders as $t) { ?> <li> <a href="<?php echo $t->url; ?>"><?php echo $t->title; ?></a> <br> <p>Status: <span class="open">Open</span></p> </li> <?php } ?> </ul> <?php } ?> This is the snippet from the homepage. The code should check if there is an open tender available, if true = display it and if false just output echo "There are no tenders available". Tenders are on separate page tree with their own template.
      I must be doing something wrong here! Help.