OpenBayou Posted January 22, 2017 Posted January 22, 2017 This is probably a complex question so forgive me if this question doesn't sound great. I have three pages called deals,endorsements and recommendations. I have a field called 'show_on_endorsements' that searches for child pages on endorsements. How do I show fields like title, etc from deals on endorsements only when 'show_on_endorsements' is selected? Example: Home - Deals - - sub-page 1 - - sub-page 2 - Endorsements - - sub-page 1 - Recommendations - - sub-page 1 - - sub-page 2 If I create a page under Deals, I want that page to be shown on Endorsements/Sub-page 1 but only if 'show_on_endorsements' has selected that page. Thanks and I hope I made sense.
blynx Posted January 22, 2017 Posted January 22, 2017 <?php // on endorsement subpage $thatRelatedDealsPage = $pages->get("show_on_endorsements=$page"); if($thatRelatedDealsPage) { echo $thatRelatedDealsPage->title; // etc ... } Something like this? I assume: On the deal page you select the endorsement in a PageReference field - and then on the endorsement page/template you can just search which page has "this page" (= $page) selected in that field. hope that helps 3
OpenBayou Posted January 22, 2017 Author Posted January 22, 2017 Thanks for the help but it didn't work. Didn't show anything. Now I've gotten some sleep, I will try again (with visuals) in case my previous question was confusing. I have three pages: recommendations, deals and endorsements. I'm looking for a way to show a page from deals on endorsements only when selected. In this example, show the '25% off until the end of the month' under deals on wpengine on endorsements. The field is called 'deals_show_on_endorse' Thanks for the help and I hope this clears up any confusions.
blynx Posted January 23, 2017 Posted January 23, 2017 So I understood that right. I think what I provided should work, of course you have to put in your own field names / variable names. Though, I just saw that I had an error in my code above (the variable in the if condition was wrong). I corrected it - you should try it again - Maybe also try this selector with $page->id "deals_show_on_endorse={$page->id}" But as I remember right this shouldn't be necessary. 1
OpenBayou Posted January 23, 2017 Author Posted January 23, 2017 AWESOME!! Thanks so much for the help. 1
OpenBayou Posted January 23, 2017 Author Posted January 23, 2017 One more item: How do you do 'foreach'? <?php $deals_item = $pages->get("deals_show_on_endorse=$page->id");?> <?php foreach($deals_item as $deal_item) : ?> <?php echo $deals_item->title;?> <?php endforeach; ?> The above code just shows the title of endorsement page.
gebeer Posted January 23, 2017 Posted January 23, 2017 Thats because of the s in <?php echo $deals_item->title;?> It needs to read <?php echo $deal_item->title;?> 1
kongondo Posted January 23, 2017 Posted January 23, 2017 What @gebeer said. I'd be surprised if that solved your problem though since this line: <?php $deals_item = $pages->get("deals_show_on_endorse=$page->id");?> fetches one page, meaning the foreach in the next line makes no sense. 1
gebeer Posted January 23, 2017 Posted January 23, 2017 @kongondo Ok, I'm going and get a coffee EDIT: @OpenBayou Try this code <?php $deal_items = $pages->find("deals_show_on_endorse={$page->id}");?> <?php foreach($deal_items as $deal_item) : ?> <?php echo $deal_item->title;?> <?php endforeach; ?> 3
kongondo Posted January 23, 2017 Posted January 23, 2017 Oops. Pure coincidence; I'm not on your case or anything, ha! . 1
OpenBayou Posted January 23, 2017 Author Posted January 23, 2017 8 minutes ago, gebeer said: <?php $deal_items = $pages->find("deals_show_on_endorse={$page->id}");?> <?php foreach($deal_items as $deal_item) : ?> <?php echo $deal_item->title;?> <?php endforeach; ?> That did it. Thanks again!
adrian Posted January 23, 2017 Posted January 23, 2017 I know code syntax choices can be a personal thing, but take a look at how much simpler this version is to read: foreach($pages->find("deals_show_on_endorse={$page->id}") as $deal_item) { echo $deal_item->title; } or if you want a one liner, even this: foreach($pages->find("deals_show_on_endorse={$page->id}") as $deal_item) echo $deal_item->title; PS - not judging here, just want to make sure you are aware of other options. I am not sure if you are just new to PW or also new to PHP. 1
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