Jump to content

How to create a (simple?) news list?


ashrai
 Share

Recommended Posts

Hey folks,

with your help, I created my first processwire page with several templates and an automated main navigation, which builds "itself" based on its children, editable text and editable images. Yeah! :)

Now the next step:
I've got 3 News-Items on my homepage. Now I want it to grab the first 3 children of a (not listed, because I'll build the news-archive later on) news-page and display two of their fields ("headline" and "teasertext") within my news-boxes.

The problem is that I can't automate the process, because every newsbox has a slightly different CSS Style for proper positioning. So foreach($blabla as $blabla)... won't work.

What would be the best approach to do realize these three newsboxes as simple as possible?

Link to comment
Share on other sites

Hi ashrai,

foreach works, you can give your news-boxes different classes based on the iteration index.

For example:

$news = $pages->get('/news-parent/')->children('limit=3, sort=-created');
foreach ($news as $k => $n) {
  echo "<div class='box{$k}'>";
  echo "<h2>{$n->headeline}</h2>";
  echo $n->teasertext;
  echo "</div>";
}

And in your css, style your boxes:

.box0 { /*first box*/ }
.box1 { /*second box*/ }
.box2 { /*third box*/ }
  • Like 2
Link to comment
Share on other sites

As with everything in PW there are many ways of doing anything. One that I think would work (untested, written in browser) would be something like

$child = $page->child; // 1st child of page

// some html here
echo $child->headline;
// more html
echo $child->teasertext;
// more html

$child = $child->next; // next child

// some html here
echo $child->headline;
// more html
echo $child->teasertext;
// more html

// and so on

<edit>Wanze beat me to it, so take your pick :) </edit>

Edited by DaveP
  • Like 2
Link to comment
Share on other sites

Thank you so much!! :)

This is great! Ah ... obviously I'm at the very beginning. Never thought about using the iteration index (never knew there was such a thing ^^).
I'll give it a try! Okay, let's boldly go where no graphics designer has gone before  ... at least me. ;)

//edit:
is it possible to sort the news by "manual drag and drop"? (sort= ???)

And how do I get get the date a page was created? maybe $page->created ... ah, where' s the cheatsheet

  • Like 1
Link to comment
Share on other sites

This is where pages are your friends

It is worth splitting everything up on to pages somewhere if you are going to treat them in different ways. Then you can just go and grab them and so what you want.

And, as pages are part of the page list, then you can drag and drop them.

You can grab ->created and ->modified. If you are using a find() system (see selectors in the API) then you can sort by date. You can also create a data field which will allow you to intentionally re-date things (for instance if you are importing archives).

So here are some options:

  • You can create pages just as children in the page tree and have them appear on the menu
  • You can create pages as children of a hidden page on the page tree (they wont appear on the menu) then import them into a template using $pages (see cheatsheet)
  • You can create a multi Page field, choose the asm type on the input tab, then manually choose pages from a parent and drag and drop the order to your hearts content.
  • You can create pages anywhere you like but have a common template then import them using $pages->find("template=mytemplate, sort=-created, limit=10"); and then loop through them
  • you can ... oh, loads of other ways.

You can have fun, basically.

  • Like 2
Link to comment
Share on other sites

"You can create pages as children of a hidden page on the page tree (they wont appear on the menu) then import them into a template using $pages (see cheatsheet)"

That's what I've done - works fine! :)
I forgot to ask two questions:
How can I output the date a page was created? Just echo $newsitem->created? And can I change the output format to DD.MM.YYYY?

And how can I create the data-field to re-date pages?
Oh my head feels like a mistreated gearbox ^^ Thanks for your patience! All will be good in a few months ;)

 

Link to comment
Share on other sites

How can I output the date a page was created? Just echo $newsitem->created? And can I change the output format to DD.MM.YYYY?

Like Joss said, you can create your own date field(s) and have them automatically format however you want. This is configured from your date field settings. But 'created' and 'modified' are built-in date fields that have no default formatting options. However, they are still very easy to output in the manner you asked about using PHP's date() function. For instance, if you wanted to output 'created' in the DD.MM.YYYY format, you would do this:

echo date('d.m.Y', $page->created); 
  • 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...