Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2007-05-24 14:40:07

frippz
Member
From: Sweden
Registered: 2005-01-30
Posts: 22
Website

Listing months restricted to current year

I’m trying to create a list of months and years that will link to specific sections of my archive showing old articles. I’d like to keep that list very simple. Just the latest months this year and below that all the years, like this.

May | Apr | Mar | Feb | Jan

2007 | 2006 | 2005 | 2004 |
2003 | 2002 | etc…

I figured out how to list the years, no problem there (I think) thanks to The Bomb Site. But I’m not sure how to restrict the listing of months to only include the current year. I don’t want it to just show the last five or six months.

Any ideas folks?


//Fredrik Frodlund

frippz.se | Adjust.nu

Offline

#2 2007-05-24 16:36:54

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,091
Website GitHub Mastodon Twitter

Re: Listing months restricted to current year

I guess you checked out this discussion dealing with monthly archives.

Last edited by colak (2007-05-24 16:38:40)


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#3 2007-05-24 17:25:50

frippz
Member
From: Sweden
Registered: 2005-01-30
Posts: 22
Website

Re: Listing months restricted to current year

I have, and it doesn’t do what I want. Plus, I have to deal with over 12000 posted articles so I need something that doesn’t try to parse the entire database while building an archive (like rss_suparchive).

Thanks anyway.

Last edited by frippz (2007-05-24 17:28:06)


//Fredrik Frodlund

frippz.se | Adjust.nu

Offline

#4 2007-05-24 19:48:17

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: Listing months restricted to current year

You’ll need a plugin, and it’s quite a block of code ;) but then this seems to work, and it will only display a month if it has articles in it:

<txp:asy_wondertag><txp:article_custom month="<txp:php>echo date("Y");</txp:php>-12" form="month" /></txp:asy_wondertag>
<txp:asy_wondertag><txp:article_custom month="<txp:php>echo date("Y");</txp:php>-11" form="month" /></txp:asy_wondertag>
<txp:asy_wondertag><txp:article_custom month="<txp:php>echo date("Y");</txp:php>-10" form="month" /></txp:asy_wondertag>
<txp:asy_wondertag><txp:article_custom month="<txp:php>echo date("Y");</txp:php>-09" form="month" /></txp:asy_wondertag>
<txp:asy_wondertag><txp:article_custom month="<txp:php>echo date("Y");</txp:php>-08" form="month" /></txp:asy_wondertag>
<txp:asy_wondertag><txp:article_custom month="<txp:php>echo date("Y");</txp:php>-07" form="month" /></txp:asy_wondertag>
<txp:asy_wondertag><txp:article_custom month="<txp:php>echo date("Y");</txp:php>-06" form="month" /></txp:asy_wondertag>
<txp:asy_wondertag><txp:article_custom month="<txp:php>echo date("Y");</txp:php>-05" form="month" /></txp:asy_wondertag>
<txp:asy_wondertag><txp:article_custom month="<txp:php>echo date("Y");</txp:php>-04" form="month" /></txp:asy_wondertag>
<txp:asy_wondertag><txp:article_custom month="<txp:php>echo date("Y");</txp:php>-03" form="month" /></txp:asy_wondertag>
<txp:asy_wondertag><txp:article_custom month="<txp:php>echo date("Y");</txp:php>-02" form="month" /></txp:asy_wondertag>
<txp:asy_wondertag><txp:article_custom month="<txp:php>echo date("Y");</txp:php>-01" form="month" /></txp:asy_wondertag>

Form ‘month’ should have something like this plus the link to your archive page (but I assume you know that):

<txp:if_different>
<txp:posted format="%b" /> 
</txp:if_different>

Offline

#5 2007-05-24 20:14:25

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Listing months restricted to current year

Hmm… if the problem is just the generating of the month/year links and not the actual archive pages for those years/months, then perhaps this gives you an idea on how to do it (not tested):

Displaying the months:

<txp:php
  $months = safe_column('MONTH(Posted) as month', 'textpattern', 'YEAR(Posted) = YEAR(NOW()) GROUP BY month');
  foreach($months as $month) {
    echo date('M', mktime(0,0,0, $month));
  }
</txp:php>

Displaying the years (except current year):

<txp:php
  $years = safe_column('YEAR(Posted) as year', 'textpattern', 'YEAR(Posted) != YEAR(NOW()) GROUP BY year');
  foreach($years as $year) {
    echo date('Y', mktime(0,0,0,0,0, $year));
  }
</txp:php>

Offline

#6 2007-05-24 20:30:33

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: Listing months restricted to current year

I see it’s time I started learning some php ;)

Offline

#7 2007-05-24 22:00:14

mikeg
Member
From: Edmonton, Canada.
Registered: 2005-02-21
Posts: 31
Website

Re: Listing months restricted to current year

This may not be exactly what you’re looking for, but the plugin upm _date_archive might work nicely. It generates a definition list with years (<dt>) and months (<dd>). You could mess around with the css to present the dl’s horizontally…

Just an idea…


“I love Beethoven. Especially the poems.”
- Ringo Starr

Offline

#8 2007-05-25 07:16:49

frippz
Member
From: Sweden
Registered: 2005-01-30
Posts: 22
Website

Re: Listing months restricted to current year

mikeg wrote:

This may not be exactly what you’re looking for, but the plugin upm _date_archive might work nicely. It generates a definition list with years (<dt>) and months (<dd>). You could mess around with the css to present the dl’s horizontally…

I will be using upm_date_archive for the actual archive page. Right now, I’m just looking to create some dynamic links for the front page.

I’ll have a try at the solutions posted by everyone and get back to you. Thanks for your help so far. =)

ruud:
The month snippet worked a treat. I just need it localized (swedish) and everything will be peachy. ;)

edit: strftime(); did the trick. =)

Last edited by frippz (2007-05-25 08:02:11)


//Fredrik Frodlund

frippz.se | Adjust.nu

Offline

#9 2007-05-25 14:37:33

frippz
Member
From: Sweden
Registered: 2005-01-30
Posts: 22
Website

Re: Listing months restricted to current year

ruud:
This is very odd. The earliest article was posted in 1997, yet the year 1996 show up in the list using your code. Any ideas what I should look for when trying to fix this?

<txp:php
  $years = safe_column('YEAR(Posted) as year', 'textpattern', 'YEAR(Posted) != YEAR(NOW()) GROUP BY year');
  foreach($years as $year) {
    echo date('Y', mktime(0,0,0,0,0, $year));
  }
</txp:php>

//Fredrik Frodlund

frippz.se | Adjust.nu

Offline

#10 2007-05-25 15:36:01

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Listing months restricted to current year

The first article, was that posted very early in January 2007? The MySQL query uses the times as stored in the database, which is either UTC or the timezone set on the server itself. It could be a timezone issue.

Offline

#11 2007-05-25 16:12:47

frippz
Member
From: Sweden
Registered: 2005-01-30
Posts: 22
Website

Re: Listing months restricted to current year

Yeah, I noticed that the years were offset by 1, so I added a +1 to $year and everything’s a ok now!


//Fredrik Frodlund

frippz.se | Adjust.nu

Offline

#12 2007-05-25 16:55:39

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Listing months restricted to current year

Ah, the bug is in the mktime line. That should be (day and month set to 1 instead of 0):

echo date('Y', mktime(0,0,0,1,1, $year));

Offline

Board footer

Powered by FluxBB