Jump to content

How do show pages on another page by a field?


OpenBayou
 Share

Recommended Posts

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.

Link to comment
Share on other sites

<?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

  • Like 3
Link to comment
Share on other sites

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.

post1.jpg

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.

post2.jpg

The field is called 'deals_show_on_endorse'

post3.jpg

Thanks for the help and I hope this clears up any confusions.

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

  • 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...