Jump to content

page refence field usage


kkalgidim
 Share

Recommended Posts

Hi,

On my web site i am using only location for countries and also cities or states

  • Locations
    • Turkey
    • Mexico
    • USA
    • Istanbul
    • Ankara
    • Newyork
    • Chicago

I am using page refence field as "sublocation"

For ex: Istanbul located in Turkey

I edit Turkey and add Istanbul as "sublocation"

when i call code below i see all the related cities with Turkey

Quote

foreach($page->sublocation as $s) {
  echo "<li><a href='$s->url'>$s->name</a></li>";
 } ?>

 

What i want is if i am on city page how can i call Turkey (parent of city) and other related cities (siblings of city) ?

If  i am on Istanbul Page i want a section called Istanbul related locations.  Turkey, Ankara will be listed on it.

 

Link to comment
Share on other sites

On Istanbul template, assuming location is the template of Turkey:

$relatedSublocationsMinusCurrent = $pages->get("template=location,sublocations=$page->id")->sublocations->remove($page);

Not tested but should work!

Link to comment
Share on other sites

3 hours ago, kkalgidim said:

if i am on city page how can i call Turkey (parent of city)

You are saying "parent" here, but according to your explanation you are not looking for the "parent" (in the PW meaning), but for the page containing the reference to istanbul, correct? Such things depend a lot on your setup of the page tree. This might be a good read:

If you have a setup like this (I guess that's your setup?)

/countries (tpl=countries)
/countries/austria (tpl=country, referenced cities: vienna,salzburg)
/countries/turkey (tpl=country, cities: istanbul, ankara)

/cities (tpl=cities)
/cities/vienna (tpl=city)
/cities/salzburg
/cities/istanbul
/cities/ankara

And you visit page "vienna" the selectors could be:

$page; // vienna
$country = $pages->findOne([
  'template' => 'country',
  'cities' => $page,
]);
$siblings = $country->cities("id!=$page");

But it might make more sense (or might not) to sturcture your pages like this:

/countries (tpl=countries)
/countries/austria (tpl=country)
/countries/austria/vienna (tpl=city)
/countries/austria/salzburg
/countries/turkey
/countries/turkey/ankara
/countries/turkey/istanbul

Then it would be:

$page; // austria
$country = $page->parent;
$siblings = $page->siblings("id!=$page");

This setup would have the benefit, that you can never assign one city to two countries. Well - if that is what you need, the other approach could make more sense (https://en.wikipedia.org/wiki/List_of_divided_cities)

@elabx s is also correct, but $page->id is actually not necessary, because when you put your $page variable in the selector string, it will automatically return the page id 😉 Other than that it's a matter of preferance if you use array syntax or not or if you use "id!=$page" or ->remove($page) 🙂 

  • Like 3
Link to comment
Share on other sites

Thanks for your help @elabx and @bernhard

Here is my solution just with location template and sublocation page reference field

Quote

                                <?php 
                                        if ($page->sublocation->eq(0)){ /* IF PAGE IS COUNTRY*/
                                            foreach($page->sublocation as $s) {
                                                echo "<li><a href='$s->url'><i class='icon_documents_alt'></i>$s->title</a></li>";/*ECHO ALL SUBLOCATIONS ON THAT COUNTRY*/
                                            } 
                                        }    
                                        
                                        else {    /* IF PAGE IS CITY OR ANY LOCATION IN A COUNTRY */
                                            $relatedCountry = $pages->get("template=location, sublocation={$page->id}"); /*GET THE RELATED COUNTRY*/
                                            $relatedCountry->sublocation->remove($page); /*REMOVE CURRENT CITY FROM LIST*/
                                                echo "<li><a href='$relatedCountry->url'><i class='icon_documents_alt'></i>$relatedCountry->title</a></li>"; /* ECHO RELATED COUNTRY*/
                                            foreach($relatedCountry->sublocation as $x) {
                                                echo "<li><a href='$x->url'><i class='icon_documents_alt'></i>$x->title</a></li>"; /*ECHO ALL SUBLOCATIONS OF THAT COUNTRY EXCEPT CURRENT CITY*/
                                            } 
                                        }
                                    ?>

 

May be it helps someone else. 

  • Like 1
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...