Jump to content

Get Repeater Fields from other pages


Kay Lohn
 Share

Recommended Posts

Hi,

Can someone please help me with the scenario below, I am trying to build a Project portfolio sort of a site.

I have a homepage which displays three recent items from a Repeater field named my_items (3 RepeaterReady fields are created for the home page and filled with data), example code below:

<?php foreach ($page->my_items as $item) { ?>

    // display item values, for example:
 
      <div class="item">
      <img src="<?php echo $item->my_image->url; ?>" />
      <a href="<?php echo $item->my_link->name; ?>">View</a>
      </div>

<?php }; ?>

I have a page called my-items with repeater 'my_items' field for displaying the same as above.

The page 'my-items' has children, each of which have 'my_items' Repeater field for displaying data respective to that page using the same code as above.

The page/ site hierarchy is like:

Home - uses Repeater 'my_items'

   |- My Items  - uses Repeater 'my_items'

       |- Item 1  - uses Repeater 'my_items'

       |- Item 2  - uses Repeater 'my_items'

       |- Item 3  - uses Repeater 'my_items'

       |- ...

   |- My Items 2

       |- Item 1  - uses Repeater 'my_items'

       |- Item 2  - uses Repeater 'my_items'

       |- Item 3  - uses Repeater 'my_items'

       |- ...       

and so on.

What I am doing as a starter with PW is:

- Entering same data for in Repeater for Home for some item, lets call this 'A'.

- Entering the same for My Items the data for 'A'

- Entering the same for Item 1 the data for 'A'

Therefore, I am adding 'A' thrice in three places, violating the DRY principle.

What I have tried and failed is:

- To get 'A' to display in My Items page fetched from Item 1 and then,

- the Home to display 'A' fetched from My Items so that I only enter data for 'A' in Item 1 and not in every page that displays it.

Caveats:

- The homepage displays 3 recent only from page My Items

- Page My Items displays one from each children

- Item 1 has multiple Repeater blocks/ pages for displaying data (e.g. My Item #1, My Item #2, My Item #3 .......... My Item #n)

Another way I think to explain it would be a photography site with structure as:

Home - display 3 recent photo projects

    |- Projects - displays each child project with the 1st photo and its description

        |- Nature - Has many photos with description

        |- Animals - Has many photos with description

        |- People - Has many photos with description

I hope I had explained properly. Any help or suggestion would be highly appreciated.

Link to comment
Share on other sites

don't have much time, but does this help:

<?php foreach ($page->my_items as $item) { ?>
    
    <div class="item">
    	<img src="<?php echo $item->my_image->url ?>" />
  		<a href="<?php echo $item->my_link->name ?>">View</a>
    </div>

<?php } ?>
Link to comment
Share on other sites

UPDATE:

For the Home page I managed to get 3 unsorted results displayed from the Projects page (I am following the example of a photography project website as in my previous post):

<?php
    $projects = $pages->get('/projects/')->my_items->getArray();
    foreach ($projects as $key => $project) { 
	?>
    <div class="item">
        <img src="<?php echo $project->my_image->url; ?>" />
        <a href="<?php echo $project->my_link->name; ?>">View</a>
    </div>
        
    <?php 
	if ($key == 2) {
        break;}
    };   
?>

- How do I sort $projects array by date?

   -The 'Projects' should be recent three.

- How do I limit get/ array?

   - If there be 1000 projects, the code would unnecessarily get an array of 1000 items whereas it only needs three items from the Projects page.

- Is there a better way to do this?

Any help would be highly appreciated. Thanks.

Link to comment
Share on other sites

don't have much time, but does this help:

<?php foreach ($page->my_items as $item) { ?>
    
    <div class="item">
    	<img src="<?php echo $item->my_image->url ?>" />
  		<a href="<?php echo $item->my_link->name ?>">View</a>
    </div>

<?php } ?>

No. This doesn't work. Sorry, I omitted the PHP open/ close tags in my original post. The code is there for reference only. Thanks for the pointer, I'll add the tags to the Original post to avoid confusion.

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

×
×
  • Create New...