louisstephens Posted January 20, 2016 Share Posted January 20, 2016 **Let me try to clear up what I am trying to do. Ill start by breaking the frontend up from the backend. Backend: I have a pageField (that is inside a repeater with 3 other fields) that allows a user to select only one page from the page tree which the template will use to grab the link to: <a href="$page->pageField->url;>Link to Selected Page</a> So basically, the repeater is set like: pageRepeater - FieldOne - FieldTwo - FieldThree - pageField Front End (template): My template was set like: <?php foreach($page->pageRepeater as $myVariable) { echo "<img src='{$myVariable->fieldOne->url}' class='img-responsive' />"; echo "<p>{$myVariable->fieldTwo} AS SHOWN</p>"; echo "<p>{$myVariable->fieldThree}</p>"; echo "<a href='{$myVariable->pageField->url}'>Link to Selected Page</a>"; } ?> All of this worked quite well, however I ran into one small issue. Occasionally, the user will not need the pageField, and will not won't the "Link to Selected Page" to display on site. I had thought that I could use an if/else statement to display the link only if a page is selected. Which is why I was trying to figure out if a page was selected. 1. check if a page is selected using pageField 2. if a page is selected: "grab the pages url" and display the link on the frontend. 3. if a page is not selected: do not display the link on the frontend I should have been a lot more clear when I originally wrote the post, but I had a lot running through my head regarding the if/else statement. I have updated the original post with this information. Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted January 20, 2016 Share Posted January 20, 2016 Could you please pick one: a) You need to check whether the page field has ANY value or b) You need to check whether the page field has a specific value (a specific page) or c) You need to check whether the page field even exist on the page's template. If a) or b) please check the Details tab of you field to see how is the field presented in the API - as an array or single page. 1 Link to comment Share on other sites More sharing options...
louisstephens Posted January 20, 2016 Author Share Posted January 20, 2016 I am sorry Ivan, I should have clarified a lot more on that. I need to check to see whether a page has been selected using the pagefield and it is set to "Single Page or empty page when none selected" Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted January 20, 2016 Share Posted January 20, 2016 if ($page->page_field->id == 123) { echo $page->page_field->pageUrl; } Maybe something like this? 2 Link to comment Share on other sites More sharing options...
kongondo Posted January 20, 2016 Share Posted January 20, 2016 (edited) if($page->your_page_field->has($p));// // if that doesn't work. if($page->your_page_field->has($p->id)); Typing from memory so there could be errors.. Edited January 20, 2016 by kongondo missing brackets Link to comment Share on other sites More sharing options...
louisstephens Posted January 20, 2016 Author Share Posted January 20, 2016 Hmm, Ill have to play around with this. foreach($page->pageField as $testing) { echo "<div class='mix col-xs-12 col-sm-6 col-md-3 '><img src='{$page->pageField->url}' class='img-responsive' />"; if($page->pageField->has($page)){ echo "test"; } else { echo "bob"; } } It seems like it is not outputing anything at all now (currently do not have any page selected). Link to comment Share on other sites More sharing options...
kongondo Posted January 20, 2016 Share Posted January 20, 2016 That code won't work... First, grab some pages, then check if they are selected in the current (or some other) page's pagefield: $pagesPotentiallySelectedInNameOfYourPageField = $pages->find('template=products, limit=50'); $someOtherPageWithNameOfYourPageField = $pages->get(1234); foreach ($pagesPotentiallySelectedInSomePageField as $p) { if($page->name_of_your_page_field->has($p)) echo 'yes, am right here, wohoo!<br>'; //if($someOtherPageWithNameOfYourPageField->name_of_your_page_field->has($p)) echo 'yes, am right here, wohoo!<br>'; else echo 'not in here!<br>'; } http://cheatsheet.processwire.com/?filter=has Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted January 21, 2016 Share Posted January 21, 2016 Well, I am in confusion. I cannot understand what are you trying to do Could you write a bit more about you case? 1) What fields does the $page we are writing output for have? I assume it does have a Page Field named pageField, is that right? 2) What pages are allowed to be selected for pageField? 3) Is that right that you can only select 1 page for pageField (as you stated before)? And so on... Please provide more details. Link to comment Share on other sites More sharing options...
louisstephens Posted January 21, 2016 Author Share Posted January 21, 2016 I do apologize Ivan. Let me try to clear up what I am trying to do. Ill start by breaking the frontend up from the backend. Backend: I have a pageField (that is inside a repeater with 3 other fields) that allows a user to select only one page from the page tree which the template will use to grab the link to: <a href="$page->pageField->url;>Link to Selected Page</a> So basically, the repeater is set like: pageRepeater - FieldOne - FieldTwo - FieldThree - pageField Front End (template): My template was set like: <?php foreach($page->pageRepeater as $myVariable) { echo "<img src='{$myVariable->fieldOne->url}' class='img-responsive' />"; echo "<p>{$myVariable->fieldTwo} AS SHOWN</p>"; echo "<p>{$myVariable->fieldThree}</p>"; echo "<a href='{$myVariable->pageField->url}'>Link to Selected Page</a>"; } ?> All of this worked quite well, however I ran into one small issue. Occasionally, the user will not need the pageField, and will not won't the "Link to Selected Page" to display on site. I had thought that I could use an if/else statement to display the link only if a page is selected. Which is why I was trying to figure out if a page was selected. 1. check if a page is selected using pageField 2. if a page is selected: "grab the pages url" and display the link on the frontend. 3. if a page is not selected: do not display the link on the frontend I should have been a lot more clear when I originally wrote the post, but I had a lot running through my head regarding the if/else statement. I have updated the original post with this information. Link to comment Share on other sites More sharing options...
Martijn Geerts Posted January 21, 2016 Share Posted January 21, 2016 // You need to to have the setting set: return null page if none selected. // When there's no page, a nullpage is returned, that page has ID 0, thus evaluates to false if ($myVariable->pageField->id) echo "<a href='{$myVariable->pageField->url}'>Link to Selected Page</a>"; 4 Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted January 22, 2016 Share Posted January 22, 2016 Come on! Martijn steps in and gets all the likes. But what about a man who did all the diagnosis . Just kidding, glad you got the question solved . 5 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted January 22, 2016 Share Posted January 22, 2016 Thanks Ivan for making me smile You just have made my day a little bit better ! 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now