Problems to get homelink for childfolder

Hi,
I have a little bit problems to set a homepage link for a complex site situation.
My sitearchitecture is:

01-home
02-cityfoo (home of cityfoo)
01-concept
02-imprint

03-citybar (home of citybar)
01-concept
02-imprint

from home I have links to the both cities. Both city-folders are enclosed websites for it own.
If I’m on one of the city websites I need a homelink but not to the main homefolder but on the home of every city (home of …).
I tried several variations but nothing worked fine or dynamic. Because I need to add new cities from the panel.

Cheers.

Is there anything those city homepages have in common, like do they share the same template?

Hi,
yes it have the template “home_hotel”.

Then you could check in your header (or wherever you want to place that link) if the template of the page (or if is is a child page, the parent of the page) uses this template. Something like:

<?php 
if($page->intendedTemplate() == 'home_hotel' OR $page->parents()->last()->intendedTemplate == 'home_hotel'): ?>
  <?php if($page->depth() > 1)
    <a href="<?= $page->parents()->last()->url() ?>"><?= $page->parents()->last()->title() ?></a>
  <?php else: ?>
    <a href="<?= $page->url() ?>"><?= $page->title() ?></a>
  <?php endif ?>
<?php endif ?>

( not sure this is the easiest way to achieve this, though)

1 Like

Hi,
actually I have now to update something on this website and now I got for this an error:
Call to a member function intendedTemplate() on null
All the last time the website worked without problems and now I installed it locally to make some changes but I get this error.

This is the code:

<?php foreach($site->languages() as $key => $language): ?>
 <?php
     if($page->intendedTemplate() == 'home_hotel' OR $page->parents()->last()->intendedTemplate() == 'home_hotel'):
         if($page->city()->value() == 'Mannheim'):
             if($key === 'cn') :
                 break;
             endif;
         endif;
      endif;
?>

Anyone an idea whats now wrong btw. could changed?

Cheers

Seems like you are using $page->intendedTemplate() in a context where $page isn‘t set or at least isn’t a page object.

Does the error message point you to a specific file and line of your code? Is it the code you posted above?

The problematic code is the OR condition, it will throw an error if the page doesn’t have parents. Solution:

if($page->intendedTemplate() == 'home_hotel' || $page->parents()->count() && $page->parents()->last()->intendedTemplate() == 'home_hotel'):
 // rest of code
endif;
1 Like

Hey,
thank you very much.
But I don’t understand why this error kills my site only on local. Its an error not an notice. Well now it works :blush: .

I have no idea what is different on your remote and localhosts, but in any case, the new solution is the correct way of doing this, even if the remote server doesn’t complain.

Because otherwise, if the first condition returns false and there are no parent pages, the second condition will throw the error, because the page object is missing.