melissa_boyle Posted January 16, 2014 Share Posted January 16, 2014 Hi guys, I have created a basic blog archive using some code from this article which works fine on showing the amount of posts for each month and year: http://processwire.c...blogs-sections/ On clicking on the urls they bring me to a page blog/year/month however shows all blog items as opposed to just those in that year. Does anyone have any ideas? My code can be seen below which posts the month and amount of articles, I need to call the excerpts for each page. The code can be seen below: <?php $channel = $pages->get("/blog/"); $startYear = date("Y"); // this year $endYear = 2009; // or whenever you want it to end $now = time(); for($year = $startYear; $year >= $endYear; $year--) { for($month = 12; $month > 0; $month--) { $startTime = strtotime("$year-$month-01"); // 2011-12-01 example if($startTime > $now) continue; // don't bother with future dates if($month == 12) $endTime = strtotime(($year+1) . "-01-01"); else $endTime = strtotime("$year-" . ($month+1) . "-01"); $entries = $channel->children("created>=$startTime, created<$endTime"); // or substitute your own date field $date = date("F, Y",$startTime); $url = "/blog/" . date("Y",$startTime) . "/" . date("m",$startTime); $count = count($entries); if($count > 0) echo "<p><a class='article-list__link' href='$url'>$date <b>(" . $count . ")</b></a></p>"; // output the month and count } } ?> Any help would be greatly appreciated, Mel Link to comment Share on other sites More sharing options...
Craig Posted January 16, 2014 Share Posted January 16, 2014 Hey Mel On clicking on the urls they bring me to a page blog/year/month however shows all blog items as opposed to just those in that year. Does anyone have any ideas? My code can be seen below which posts the month and amount of articles, I need to call the excerpts for each page. I've highlighted two bits above in bold - is it right that you have those two problems? The last one should be quite easy. ... $count = count($entries); if($count > 0) { echo "<p><a class='article-list__link' href='$url'>$date <b>(" . $count . ")</b></a></p>"; // output the month and count echo "<p><ul>"; foreach ($entries as $entry) { echo "<li>"; echo "<a href='$entry->url'>$entry->title</a>"; echo "<p>$entry->summary</p>"; echo "</li>"; } echo "</ul></p>"; } What that does is just loop through the $entries PageArray. Each $entry is a page, so you can access all of its fields. You can change the HTML and field references to suit your pages. You say clicking on the /news/year/month/ URLs is showing all blog posts, and not those in the year and month. I presume the template file for the template of the /news/ page has some code in it that pulls out your blog posts? You didn't mention any more details or code relating to this bit, so I'm guessing a bit now - but here are some things to check. The template of the /news/ page. In the CMS, go to edit the Template of this page. The "Allow URL Segments" option should be ticked in the URLs tab. In the template file for that template, you should be able to use $input->urlSegment(1) to retrieve the year and $input->urlSegment(2) for the month. Something like this in that template should get you started: // Get year and month from URL $year = $input->urlSegment(1); $month = $input->urlSegment(2); if ($year && $month) { // Both year and month $startTime = strtotime(date("$year-$month-01")); $endTime = strtotime(date("$year-$month-31")); // Your /blog/ page $channel = wire('pages')->get('/blog/'); $entries = $channel->find("date>=$startTime, date<=$endTime"); // or substitute your own date field $count = count($entries); if($count > 0) { echo "<p><ul>"; foreach ($entries as $entry) { echo "<li>"; echo "<a href='$entry->url'>$entry->title</a>"; echo "<p>$entry->summary</p>"; echo "</li>"; } echo "</ul></p>"; } else { echo "<p>No posts found.</p>"; } } Link to comment Share on other sites More sharing options...
kongondo Posted January 16, 2014 Share Posted January 16, 2014 (edited) Hi Mel, As a btw, don't know if you are aware PW has a blog profile (module). Archive example 1 and example 2 Edited January 16, 2014 by kongondo Link to comment Share on other sites More sharing options...
melissa_boyle Posted January 17, 2014 Author Share Posted January 17, 2014 Hey Craig, Thank you so much for your time and help- this code totally makes sense although it doesn't seem to work for me. I have installed the date archiver which groups the posts accordingly, on viewing the 2014 page it shows the month_year template, this is where I have used the code you suggested. I have shown the structure of the posts below and have also pasted the month_year template code. I am sure I am doing something silly Blog2 20132 111 Aliquam tristique lobortis sem 081 20141 <?php include("./impact_head.inc"); include("./impact_inside_head.inc"); ?> <div class="feature-box"> <div class="feature-box__item grid__item one-whole palm-one-whole flush"> <hr class="feature-box__divider"> <h4 class="feature-box__title">Blog</h4> <div class="article-list"> <?php // Get year and month from URL $year = $input->urlSegment(1); $month = $input->urlSegment(2); if ($year && $month) { // Both year and month $startTime = strtotime(date("$year-$month-01")); $endTime = strtotime(date("$year-$month-31")); // Your /blog/ page $channel = wire('pages')->get('/blog/'); $entries = $channel->find("date>=$startTime, date<=$endTime"); // or substitute your own date field $count = count($entries); if($count > 0) { echo "<p><ul>"; foreach ($entries as $entry) { echo "<li>"; echo "<a href='$entry->url'>$entry->title</a>"; echo "<p>lol $entry->summary</p>"; echo "</li>"; } echo "</ul></p>"; } else { echo "<h4>No posts found.</h4>"; } } ?> </div> <div class='right_archive'> <h4 class="article-list__title--blog">Archive</h4> </div> </div> </div> <?php include("./impact_foot.inc"); include("./impact_inside_foot.inc"); ?> Again thank you so much for your help! Mel 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