OpenBayou Posted January 24, 2017 Posted January 24, 2017 Is it possible to group pages by a field? For example: show all pages in a group that were written by a date. Like the following: Today - page A - page B Yesterday - page C - page D Thanks again for the help.
Macrura Posted January 25, 2017 Posted January 25, 2017 You would need a date field on the 'page'. Sorting by date would be easy; outputting the date or using words like Yesterday and Today would require some if/then tests on the output listing, using date comparison; you may need to study this: http://php.net/manual/en/function.date.php
OpenBayou Posted January 25, 2017 Author Posted January 25, 2017 https://processwire.com/talk/topic/2047-group-by/#comment-19162 Got it somewhat working. <?php $pa = $pages->find("has_parent=/promos/"); $groups = array(); foreach($pa as $p) $groups["$p->podcast_shows"][] = $p->id;?> <?php echo "<ul>"; foreach($groups as $key => $gr){ $count = count($gr); echo "<li>$key"; if($count){ echo "<ul>"; foreach($gr as $key => $pid){ $r = $pages->get($pid); echo "<a href='$r->url'>$r->title</a>"; } echo "</ul>"; } echo "</li>"; } echo "</ul>";?> The problem is this line echo "<li>$key"; It shows the page ID and I need to show the title. I tried '$key->title' and that didn't work. 1
adrian Posted January 25, 2017 Posted January 25, 2017 If you're taking this approach, you'll either need to add the title to the $groups array, or get the page from its ID before trying to echo the title.
OpenBayou Posted January 25, 2017 Author Posted January 25, 2017 Don't know where to go next. Still need help. Tried '$p->podcast_shows->title' that didn't work. Tried '$pages->find("has_parent=/promos/")->title' and that didn't work as well.
adrian Posted January 25, 2017 Posted January 25, 2017 3 hours ago, OpenBayou said: Tried '$p->podcast_shows->title' that didn't work. Not meaning to be too critical here, but it would be really helpful if you could start providing a little more info. Just telling us it didn't work makes it a lot more difficult to help. It would be great if you could do a little debugging first. Do you have debug mode turned on? What about TracyDebugger - do you have that installed and the debug bar turned on? Those will both help you get more info on why something isn't working. I seem to recall in another one of your recent posts you had an issue with a multi vs single page field. Remember that you can't get the title from a multi page field like that - you need to specify which one of the selected pages. It might be as simple as $p->podcast_shows->first()->title but without more info, it's hard for us to know. 4
OpenBayou Posted January 25, 2017 Author Posted January 25, 2017 Ok, I have TracyDebugger on, have no idea what I'm looking at. Did a var dump on $key and all I got was int(1155). When do '$p->podcast_shows->title' I get internal errors. I'm sorry if I'm not making any sense. I'm not used to ProcessWire, I'm used to WordPress and I though it would be great idea to diversify my portfolio using different CMSs. I had a deadline on this last Friday and I'm working on little sleep and I can't think properly. And I never done something this complex like grouping posts and I'm not a good programmer, all I do is search for my problem and see if someone has the same the same problem and hope there's a solution. If there is, I try my best to get it working that satisfies what I need to accomplish.
adrian Posted January 25, 2017 Posted January 25, 2017 1 minute ago, OpenBayou said: I'm not a good programmer That's a relative thing - we could all say that relative to someone like Linus Torvalds. The key thing is that you are trying Can you confirm that podcast_shows is in fact a Page field? Is it set to multiple or single? Did you try my suggestion of ->first()->title? 3
OpenBayou Posted January 25, 2017 Author Posted January 25, 2017 'podcast_shows' is a field that uses the page option that's set to Single Page or boolean false. It gets childpages from 'Podcast'
Robin S Posted January 25, 2017 Posted January 25, 2017 Here is an example of grouping news items under date headings according to a "post_date" datetime field. The headings are the dates (formatted as per the "Date Output Format" specified in the datetime field settings), unless the date equates to "Today" or "Yesterday", in which case those strings are used. You should be able to adapt this to your needs. $news_items = $pages->find("template=news_item, sort=-post_date"); $previous_item_date = ''; foreach($news_items as $news_item) { if($news_item->post_date !== $previous_item_date) { $timestamp = $news_item->getUnformatted('post_date'); if($timestamp >= strtotime('today')) { $date = 'Today'; } elseif($timestamp >= strtotime('yesterday')) { $date = 'Yesterday'; } else { $date = $news_item->post_date; } echo "<h3>$date</h3>"; // output the date heading } echo "<p>$news_item->title</p>"; // output whatever fields of the item $previous_item_date = $news_item->post_date; } 2
OpenBayou Posted January 25, 2017 Author Posted January 25, 2017 Well, no matter what I do, everything is in integers. Back to the drawing board. Thanks for the help and understanding.
OpenBayou Posted January 25, 2017 Author Posted January 25, 2017 FIXED IT! Changed echo "<li>$key"; to echo $pages->get($key)->title; And it displays the title of the show. Thanks everyone for helping and understanding me. This is the final question as the project I've been working on is now complete. I'll post what I did sometime next month. Thanks again!
LostKobrakai Posted January 25, 2017 Posted January 25, 2017 There's no need for an additional $pages->get() here. $pages->find() will already retrieve those pages. $items = $pages->find("some=selector"); foreach($items as $item) { // $item === $pages->get($item->id); echo $item->title; } 2
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