Jump to content
Rebecca

How do you call data from a page or pages into another page?

Recommended Posts

Please have a look at this HTML template >> http://famousthemes.com/primely/

The design is made so that many pages seem to request lists of data that would belong to another page:

  • Featured entries on a portfolio (which call all titles, all images and all descriptions each group in it's own div, for the animation to work),

  • A list of subpages of some sort

  • Latest updates

  • Latest blog entries

  • A list of categories (which could be weighted by popularity or not)

  • A list of client testimonials.

And that's only the first page, if you explore more, you'll find that many other pages kind of do the same as well.

So, my question would be, how would you call a list of entries from a set of pages into a different page? Or rather, how do you call many different sets of pages' data into one single page template?

Thanks :D

Share this post


Link to post
Share on other sites

Hi Rebecca and welcome to the forums.

That is very easily done with PW, through simple jQuery-style API. Most of the information you need can be found from this page: http://processwire.com/api/templates/

And there is header: Finding and loading other pages from your template file. After that if you provide some more information, like what fields you need and from what page/template, I think many of us can help you further.

Share this post


Link to post
Share on other sites

  • Featured entries on a portfolio (which call all titles, all images and all descriptions each group in it's own div, for the animation to work),

  • A list of subpages of some sort

  • Latest updates

  • Latest blog entries

  • A list of categories (which could be weighted by popularity or not)

  • A list of client testimonials.

I do not know how I missed this list on first read :) Here is few examples (code is not tested and your template names are probably different):

List of featured portfolio items (I haven't actually played with images yet, but that is from example template):

$featured_from_portfolio = $pages->find("template=portfolio_item, sort=-datetime, featured=1, limit=5");

echo "<ul>";
foreach ($featured_from_portfolio as $portfolio_item) {
   $image = $portfolio_item->images->getRandom(); 
   $thumb = $image->size(500, 300); 	
   echo "<li>";
   echo "<a href='{$portfolio_item->url}'>{$portfolio_item->title}</a> ({$portfolio_item->datetime})";
   echo "<a href='{$image->url}'><img id='photo' src='{$thumb->url}' alt='{$thumb->description}' width='{$thumb->width}' height='{$thumb->height}' /></a>";
   echo "</li>";
};
echo "</ul>";

This snippet first finds pages that have "template=portfolio_item" and field called "featured" is checked. It sorts them by date (template has field called "datetime") and limits results to 5. After that we loop through these max five portfolio_items and show results. You probably would have own image field for this usage like "featured_image".

So this code assumes that you have:

  • Template called portfolio_item
  • portfolio_item has at least fields: title (it's always and cannot be removed), datetime, images, featured

Lates blog entries:

$newest_blog_posts = $pages->find("template=blogpost, sort=-datetime, limit=10");

echo "<ul>";
foreach ($newest_blog_posts as $post) {
echo "<li><a href='{$post->url}'>{$post->title}</a> ({$post->datetime})</li>";
};
echo "</ul>";

Hope this helps a little bit to right direction. I haven't actually developed any real sites with PW yet, so don't trust my word too much ;)

Share this post


Link to post
Share on other sites

Since you only seem to have one question [which apeisa strangely ommited and went for examples ;D]

how would you call a list of entries from a set of pages into a different page? Or rather, how do you call many different sets of pages' data into one single page template?

in every template, there are few global PHP variables given to you [read: http://processwire.com/api/variables/]. The one that interests us now is '$pages', which basically gives you a connection to all other pages in the site. In your templates, via $pages->get(-selector-) or $pages->find(-selector-) or one of the others [listed in documentation] you may load other pages into the one currently vied. Read more about selectors: http://processwire.com/api/selectors/

Adam

Share this post


Link to post
Share on other sites

Since you only seem to have one question [which apeisa strangely ommited and went for examples ;D]

in every template, there are few global PHP variables given to you [read: http://processwire.com/api/variables/]. The one that interests us now is '$pages', which basically gives you a connection to all other pages in the site. In your templates, via $pages->get(-selector-) or $pages->find(-selector-) or one of the others [listed in documentation] you may load other pages into the one currently vied. Read more about selectors: http://processwire.com/api/selectors/

Adam

I don't know why, but this message from Adam only showed on reply view - but topic list showed it as latest reply. Weird thing. Hope it shows now, since that is clear answer. (EDIT: Now I see Adam's message too)

I actually answered (or tried to answer :)) this question on my first post and jumped to examples in next. I think that easiest examples are on http://processwire.com/api/templates/ page and under header: Finding and loading other pages from your template file

Share this post


Link to post
Share on other sites

Apeisa's examples are great. You may also sometimes want to pull pages based on their location rather than what template they use. For example, lets say you were on the homepage (or some other page) and wanted to pull the 3 most recent news items from the /company/news/ page. I'm going to assume that the news page is already set to sort it's children by date descending.

<?php
$latestNews = $pages->get("/company/news/")->children("limit=3"); 
foreach($latestNews as $item) {
    echo "<p><a href='{$item->url}'>{$item->title}</a><br />{$item->summary}</a></p>";
}
  • Like 1

Share this post


Link to post
Share on other sites

Offtopic: This is a mystery topic. Two replys are missing (Adam's and one mine). I see them only when I click reply. Topic view shows 5 (after this one 6) replys, but there is only 3. I don't see them even if I log out or use different browser.

Share this post


Link to post
Share on other sites

Thanks for letting me know. I found one of your messages and one of Adam's stuck in the Akismet spam filter (false positives). I identified those as false positives and hopefully they are showing up now. Do you see anything else that is missing? (i.e. you mentioned two of your messages, though I only saw one in the filter).

Share this post


Link to post
Share on other sites

Yep, now it works, I don't know any more missing replies. Strange (but good in this situation) that those showed up in reply mode.

Share this post


Link to post
Share on other sites

I don't know why, I was a little surprised that it flagged them. That's the first time I've seen false positives with Akismet. But I'm guessing you are right about the links.

Share this post


Link to post
Share on other sites

Maybe combination of engrish [same same but different] and links. I sure hope spam filters don't judge comments based on english level :D

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...