x
 
Create New Page
 View Previous Changes    ( Last updated by narration )

Calendar that only hits active months

Here is a version of the standard EE Calendar tag applied with the Query module, so that it will only land on months where you have postings, when you use the forward and backwards links. It was built and tested in EE 2.0.

This plus doing some nice styling, and you’ve got a calendar that works very nicely, and doesn’t take special handling in your template for calls to empty months.

<h2 class="sidetitle">History</h2><br />
<
div id="calendar">
{exp:channel:calendar switch="calendarToday|calendarCell" channel="{my_channel}" dynamic="yes"  show_pages="no" }
<table class="calendarBG" border="0" cellpadding="5" cellspacing="1" summary="My Calendar">
<
tr>
<
th class="calendarHeader"><div class="calendarMonthLinks">
{exp:query sql="SELECT year, month FROM exp_channel_titles where status = 'open' and channel_id = '6' and ((year = {date format="%Y"} and month < {date format="%n"}) or year < {date format="%Y"}) order by year desc, month desc limit 1;" }
<a href="{path={my_template_group}/articles_all/{year}/{month}}"><<</a>
{/exp:query}</div></th>
<
th class="calendarHeader" colspan="5">{date format="%F %Y"}</th>
<
th class="calendarHeader"><div class="calendarMonthLinks">
{exp:query sql="SELECT year, month FROM exp_channel_titles where status = 'open' and channel_id = '6' and ((year = {date format="%Y"} and month > {date format="%n"}) or year > {date format="%Y"}) order by year asc, month asc limit 1;" }
<a href="{path={my_template_group}/articles_all/{year}/{month}}">>></a>
{/exp:query}</div></th>
</
tr>
<
tr>
{calendar_heading}
<td class="calendarDayHeading">{lang:weekday_abrev}</td>
{/calendar_heading}
</tr>
{calendar_rows }
{row_start}
<tr>{/row_start}
{if entries}
<td class='{switch}' align='center'><a href="{day_path={my_template_group}/articles_all}">{day_number}</a></td>{/if}
{if not_entries}
<td class='{switch}' align='center'>{day_number}</td>{/if}
{if blank}
<td class='calendarBlank'>&nbsp;</td>{/if}
{row_end}
</tr>{/row_end}
{
/calendar_rows}
</table>
{/exp:channel:calendar}
</div

If you are using table prefixes in your database, you’ll need to modify the queries in the obvious way to fit your table names.

The one thing I couldn’t get to work is the {if no_results} aspect of queries, so they are not used. I think Query is not completely happy running inside the exp:calendar tags, which results in the no_results clauses sometimes acting and sometimes not, depending on positioning and content. Quite evidently the parsing doesn’t quite work right in this case.

Without the no_results blocks, you just get a blank forward or backward link when you’re at one end of the time period of entries, which is reasonable at least.