jmn817 Posted January 13, 2017 Share Posted January 13, 2017 Hello everyone, I'm new to Processwire and so far things are going very well. However, I'm having trouble with one thing that would help dynamically grow the website. Below is my code that will get the pages under the root directory and list the pages from the specified folder. <ul id="trending"> <?php $root = $pages->get("/politics/2017/01/17/"); $children = $root->children("limit=5"); $children->prepend($root); foreach($children as $child) { echo "<li><a href='{$child->url}'>{$child->title}</a></li>"; } ?> </ul> Is there a way that I can have php automatically search for the newest folder under my directory, meaning the new date? I go by the date (yyyy/mm/dd) when organizing my pages, and it would be helpful if php automatically got the newest folder (day) without me having to change the folder each day in php. $root = $pages->get("/politics/2017/01/17/"); Thank you for the help, Jesse Link to comment Share on other sites More sharing options...
Harmen Posted January 13, 2017 Share Posted January 13, 2017 I think you need to do this with Javascript. You can get the date, and set it to a var orso and implement it in your code Link to comment Share on other sites More sharing options...
rick Posted January 13, 2017 Share Posted January 13, 2017 Welcome to the forum Jesse! Your code shows a hard-coded date (I assume for this example). Using the current date, limit the number of 'pages' to a quantity, and sort them by date. This will give you x number of pages in chronological order similar to a blog's recent post listing. Sorry for a simplistic answer. I don't have access to topic listings at the moment. Someone will post here shortly. Again, welcome aboard! 1 Link to comment Share on other sites More sharing options...
kongondo Posted January 13, 2017 Share Posted January 13, 2017 Hi @jmn817, Welcome to ProcessWire and the forums. I am assuming all these are separate pages ('folders)': politics, 2017, 01 and 17? In other words, your tree looks like/will look like? politics 2017 01// child of year 17// child of month 18 19 02 01 02 // etc 2018 If you 'day' pages are created chronologically, and not too much in advance (e.g. no day 21 before day 18), and if they have their own exclusive template, the following should achieve what you want I think. <ul id="trending"> <?php //$root = $pages->get("/politics/2017/01/17/"); // get the 'day' page that was created last. @note the -created. $root = $pages->get('template=name-of-day-template, sort=-created'); $children = $root->children("limit=5"); $children->prepend($root); foreach($children as $child) { echo "<li><a href='{$child->url}'>{$child->title}</a></li>"; } ?> </ul> 2 Link to comment Share on other sites More sharing options...
jmn817 Posted January 13, 2017 Author Share Posted January 13, 2017 @kongondo That works perfectly, thanks! Link to comment Share on other sites More sharing options...
Robin S Posted January 14, 2017 Share Posted January 14, 2017 This is just an idea... You could consider making the creation and maintenance of these articles easier for site editors by having a simpler structure in the page tree (just the categories as parent pages), entering the article date with a datetime field, and then creating the desired URL for the frontend using URL segments (enable this on the category template). So your category template would look for the following URL segments: segment 1: year - sanitize to integer, throw 404 if out of range segment 2: month - sanitize to integer, throw 404 if out of range segment 3: day - sanitize to integer, throw 404 if out of range segment 4: article page name - throw 404 if no match for article page name In terms of finding articles that match a given year, month or day you could convert to a timestamp range as Ryan shows here, but to make it easier (and considering you'll need the integers for building the article URLs) I would instead add 3 integer fields to your article template, set the field visibility to "Hidden (not shown in editor)" and then populate them with a saveReady hook. // in /site/ready.php $this->pages->addHookAfter('saveReady', function($event) { $page = $event->arguments('page'); if($page->template->name === 'article' && $page->article_date) { $page->day = date('d', $page->article_date); $page->month = date('m', $page->article_date); $page->year = date('Y', $page->article_date); } }); Now it's really easy to find articles, e.g. $matches = $pages->find("template=article, year=2016, month=11, sort=sort"); For the question in your original post you'd do something like this: $latest_article = $pages->get("template=article, parent=/politics/, sort=-article_date"); $recent_articles = $pages->find("template=article, parent=/politics/, article_date={$latest_article->article_date}, sort=sort, limit=5"); $day_link = "/politics/{$latest_article->year}/{$latest_article->month}/{$latest_article->day}"; // use this as the first link to the 'parent' page When you need to output the URL of an article you would build it like this: $url = "{$article->parent->url}{$article->year}/{$article->month}/{$article->day}/{$article->name}/"; For convenience you'd probably make a simple function that returns this URL string. In the backend, rather than browse through the page tree, if you want to list articles by day/month/year you can use the "Find" lister. Or better yet a dedicated Lister Pro instance for articles. 4 Link to comment Share on other sites More sharing options...
jmn817 Posted January 15, 2017 Author Share Posted January 15, 2017 @Robin S Don't know how I missed this suggestion, thanks! Link to comment Share on other sites More sharing options...
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