Textpattern Forum

You are not logged in. Register | Login | Help

#1 2011-05-19 03:49:14

michaelkpate
Moderator
From: Avon Park, FL
Registered: 2004-02-24
Posts: 476
Website

[howto] Date-Based Archives - Yearly, Monthly, Daily, and Complete

Some 5 1/2 year ago, I wrote:

I still it will be preferable to have monthly archive pages again someday, but until then, this will do nicely…

It has obviously taken a while but I have most of a solution now.

Two Plugins are essential to make this work: adi_gps and smd_if

1. Create a section named “archives” and use this or something very similar as the page template:

<txp:output_form form="archivetype" />

<txp:output_form form="header" />

<div id="container">

	<div id="content" role="main">

		<txp:output_form form="archivetitle" />

		<txp:output_form form="archivebody" />

	</div><!-- #content -->

</div><!-- #container -->

<txp:output_form form="sidebar" />

<txp:output_form form="footer" />

2. Create the archivetype misc form:

<txp:adi_gps />

<txp:smd_if field="txpvar:y,txpvar:m,txpvar:d" operator="isused,isused,isused">

   <txp:variable name="archivetype" value="daily" />

<txp:else />

	<txp:smd_if field="txpvar:y,txpvar:m" operator="isused,isused">

		<txp:variable name="archivetype" value="monthly" />

	<txp:else />

		<txp:smd_if field="txpvar:y" operator="isused">

			<txp:variable name="archivetype" value="yearly" />

		<txp:else />

			<txp:variable name="archivetype" value="archives" />

		</txp:smd_if>

	</txp:smd_if>

</txp:smd_if>

<txp:smd_if field="txpvar:m" operator="isused">
	<txp:php>global $variable; $month_num = variable(array("name" => "m")); $variable['monthtitle'] = date('F', mktime(0, 0, 0, $month_num));</txp:php>
</txp:smd_if>

3. Create the archivetitle misc form:

<h1 class="page-title">

<txp:if_variable name="archivetype" value="yearly">

	Yearly Archives: <span><txp:variable name="y" /></span>

</txp:if_variable>

<txp:if_variable name="archivetype" value="monthly">

	Monthly Archives: <span><txp:variable name="monthtitle" /> <txp:variable name="y" /></span>

</txp:if_variable>

<txp:if_variable name="archivetype" value="daily">

	Daily Archives: <span><txp:variable name="monthtitle" /> <txp:variable name="d" />, <txp:variable name="y" /></span>

</txp:if_variable>

<txp:if_variable name="archivetype" value="archives">

	Archives

</txp:if_variable>

</h1>

4. Create the archivebody misc form:

<txp:if_variable name="archivetype" value="yearly">

	<txp:article_custom form="archives" limit="99999" month='<txp:variable name="y" />' section="article" />

</txp:if_variable>

<txp:if_variable name="archivetype" value="monthly">

	<txp:article_custom form="archives" limit="99999" month='<txp:variable name="y" />-<txp:variable name="m" />' section="article" />

</txp:if_variable>

<txp:if_variable name="archivetype" value="daily">

	<txp:article_custom form="archives" limit="99999" month='<txp:variable name="y" />-<txp:variable name="m" />-<txp:variable name="d" />' section="article" />

</txp:if_variable>

<txp:if_variable name="archivetype" value="archives">

	<txp:article_custom form="archives" limit="99999" section="article" />			

</txp:if_variable>

5. Create the archives article form:

<a href="<txp:permlink />" title="Permanent link to <txp:title />"><txp:title /></a><br />

This will give you the basic structure. To link to a specific archive, you construct links like so:

Yearly – http://cmsstyles.com/demos/twentyten/archives/?y=2011

Monthly – http://cmsstyles.com/demos/twentyten/archives/?y=2011&m=04

Daily – http://cmsstyles.com/demos/twentyten/archives/?y=2011&m=04&d=11

Complete – http://cmsstyles.com/demos/twentyten/archives/

The code I use in the sidebar to create the links to the Monthly Archives:

<li id="archives" class="widget-container">
	<h3 class="widget-title">Archives</h3>
	<ul>
		<txp:article_custom limit="99999" form="archives widget" />
	</ul>			
</li>

and in the archives widget article form:

<txp:if_different>
	<li><a href="<txp:site_url />archives/?y=<txp:posted format="%Y" />&m=<txp:posted format="%m" />"><txp:posted format="%B %Y" /></a></li>
</txp:if_different>

The header file I use is kind of complicated but this portion is the relevant for this example.

<txp:if_variable name="archivetype" value="yearly">
	<txp:variable name="y" />  | <txp:site_name />
</txp:if_variable>

<txp:if_variable name="archivetype" value="monthly">
	<txp:variable name="monthtitle" /> <txp:variable name="y" /> | <txp:site_name />
</txp:if_variable>

<txp:if_variable name="archivetype" value="daily">
	<txp:variable name="monthtitle" /> <txp:variable name="d" />, <txp:variable name="y" /> | <txp:site_name />
</txp:if_variable>

<txp:if_variable name="archivetype" value="archives">
	Archives | <txp:site_name />
</txp:if_variable>

Further Plans: I had an idea for some error-checking but it isn’t working yet and I want to eventually do some url-rewriting. But for now, I thought it might be more productive to make it available.


Textpattern Community Member since 2004-02-23 | World Headquarters

Offline

#2 2011-05-25 19:28:46

michaelkpate
Moderator
From: Avon Park, FL
Registered: 2004-02-24
Posts: 476
Website

Re: [howto] Date-Based Archives - Yearly, Monthly, Daily, and Complete

Version 2.0 – A little less readable but uses less code to do more things

1. Create a section named “archives” and use this or something very similar as the page template:

<txp:output_form form="archivetype" />

<txp:output_form form="header" />

<div id="container">

	<div id="content" role="main">

		<h1 class="page-title"><txp:variable name="archivetitle" /></h1>

		<txp:output_form form="archivebody" />

	</div><!-- #content -->

</div><!-- #container -->

<txp:output_form form="sidebar" />

<txp:output_form form="footer" />

2. Create the archivetype misc form:

<txp:adi_gps />

<txp:smd_if field="txpvar:m" operator="isused">
	<txp:php>global $variable; $month_num = variable(array("name" => "m")); $variable['monthtitle'] = date('F', mktime(0, 0, 0, $month_num));</txp:php>
</txp:smd_if>

<txp:smd_if field="txpvar:y,txpvar:m,txpvar:d" operator="isused,isused,isused">

   <txp:variable name="archivetype" value="daily" />
   <txp:variable name="archivepagetitle" value='<txp:variable name="monthtitle" /> <txp:variable name="d" />, <txp:variable name="y" /> | <txp:site_name />' />
   <txp:variable name="archivetitle" value='Daily Archives: <span><txp:variable name="monthtitle" /> <txp:variable name="d" />, <txp:variable name="y" /></span>' />
   <txp:variable name="archivemonth" value='<txp:variable name="y" />-<txp:variable name="m" />-<txp:variable name="d" />' />

<txp:else />

	<txp:smd_if field="txpvar:y,txpvar:m" operator="isused,isused">

		<txp:variable name="archivetype" value="monthly" />
		<txp:variable name="archivepagetitle" value='<txp:variable name="monthtitle" /> <txp:variable name="y" /> | <txp:site_name />' />
		<txp:variable name="archivetitle" value='Monthly Archives: <span><txp:variable name="monthtitle" /> <txp:variable name="y" /></span>' />
		<txp:variable name="archivemonth" value='<txp:variable name="y" />-<txp:variable name="m" />' />


	<txp:else />

		<txp:smd_if field="txpvar:y" operator="isused">

			<txp:variable name="archivetype" value="yearly" />
			<txp:variable name="archivepagetitle" value='<txp:variable name="y" />  | <txp:site_name />' />
			<txp:variable name="archivetitle" value='Yearly Archives: <span><txp:variable name="y" /></span>' />
			<txp:variable name="archivemonth" value='<txp:variable name="y" />' />

		<txp:else />

			<txp:variable name="archivetype" value="archives" />
			<txp:variable name="archivepagetitle" value='Archives | <txp:site_name />' />
			<txp:variable name="archivetitle" value="Archives" />
			<txp:variable name="archivemonth" value="archives" />

		</txp:smd_if>

	</txp:smd_if>

</txp:smd_if>

3. Create the archivebody misc form:

 <txp:if_variable name="archivemonth" value="archives">

	<txp:article_custom form="archivecheck" limit="1" section="article" />

<txp:else />

	<txp:article_custom form="archivecheck" limit="1" month='<txp:variable name="archivemonth" />' section="article" />

</txp:if_variable>

<txp:if_variable name="archivecheck" value="true">

	<txp:if_variable name="archivemonth" value="archives">

		<txp:article_custom form="archives" limit="99999" section="article" />

	<txp:else />

		<txp:article_custom form="archives" limit="99999" month='<txp:variable name="archivemonth" />' section="article" />

	</txp:if_variable>

<txp:else />

	<p>No articles were created during this time period.</p>

</txp:if_variable>

4. Create the archivecheck article form:

<txp:variable name="archivecheck" value="true" />

5. Create the archives article form:

<a href="<txp:permlink />" title="Permanent link to <txp:title />"><txp:title /></a><br />

and the pagetitle code is much simpler:

<txp:if_section name="archives" >
	<txp:variable name="archivepagetitle" />
</txp:if_section>

You now get a message is there are no articles to display. The archivetitle form was replaced by a variable. I left the archivetype variable in even though I didnt’ acually use it anywhere just in case someone had a use for it – it is completely optional.


Textpattern Community Member since 2004-02-23 | World Headquarters

Offline

#3 2012-01-26 21:47:45

michaelkpate
Moderator
From: Avon Park, FL
Registered: 2004-02-24
Posts: 476
Website

Re: [howto] Date-Based Archives - Yearly, Monthly, Daily, and Complete

Version 3.0 – this one makes creative use of error pages as well as moving the php code to a plugin

1. Create a section named “archives” and use this or something very similar as the page template:

<txp:output_form form="header-archives" />

<txp:output_form form="header" />

	<div id="primary">
		<div id="content" role="main">

<txp:output_form form="task-archives" />

		</div><!-- #content -->
	</div><!-- #primary -->

<txp:output_form form="sidebar" />

<txp:output_form form="footer" />

3. the header-archives misc form:

<txp:mkp_archive />

<txp:smd_if field="txpvar:y,txpvar:m,txpvar:d" operator="isused,isused,isused">

   <txp:variable name="archivetype" value="daily" />
   <txp:variable name="archivepagetitle" value='<txp:variable name="monthtitle" /> <txp:variable name="d" />, <txp:variable name="y" /> | <txp:site_name />' />
   <txp:variable name="archivetitle" value='Daily Archives: <span><txp:variable name="monthtitle" /> <txp:variable name="d" />, <txp:variable name="y" /></span>' />
   <txp:variable name="archivemonth" value='<txp:variable name="y" />-<txp:variable name="m" />-<txp:variable name="d" />' />

<txp:else />

	<txp:smd_if field="txpvar:y,txpvar:m" operator="isused,isused">

		<txp:variable name="archivetype" value="monthly" />
		<txp:variable name="archivepagetitle" value='<txp:variable name="monthtitle" /> <txp:variable name="y" /> | <txp:site_name />' />
		<txp:variable name="archivetitle" value='Monthly Archives: <span><txp:variable name="monthtitle" /> <txp:variable name="y" /></span>' />
		<txp:variable name="archivemonth" value='<txp:variable name="y" />-<txp:variable name="m" />' />


	<txp:else />

		<txp:smd_if field="txpvar:y" operator="isused">

			<txp:variable name="archivetype" value="yearly" />
			<txp:variable name="archivepagetitle" value='<txp:variable name="y" />  | <txp:site_name />' />
			<txp:variable name="archivetitle" value='Yearly Archives: <span><txp:variable name="y" /></span>' />
			<txp:variable name="archivemonth" value='<txp:variable name="y" />' />

		<txp:else />

			<txp:variable name="archivetype" value="archives" />
			<txp:variable name="archivepagetitle" value='Archives | <txp:site_name />' />
			<txp:variable name="archivetitle" value="Archives" />
			<txp:variable name="archivemonth" value="archives" />

		</txp:smd_if>

	</txp:smd_if>

</txp:smd_if>

3. Create the task-archives misc form:

<h1 class="page-title"><txp:variable name="archivetitle" /></h1>

<txp:variable name="archivehasarticles" value="false" />

<txp:if_variable name="archivemonth" value="archives">

	<txp:article_custom limit="1" section="article" >
		<txp:variable name="archivehasarticles" value="true" />
	</txp:article_custom>

<txp:else />

	<txp:article_custom limit="1" month='<txp:variable name="archivemonth" />' section="article" >
		<txp:variable name="archivehasarticles" value="true" />
	</txp:article_custom>

</txp:if_variable>

<txp:if_variable name="archivehasarticles" value="true">

	<txp:if_variable name="archivemonth" value="archives">

		<txp:article_custom form="archives" limit="99999" section="article" />

	<txp:else />

		<txp:article_custom form="archives" limit="99999" month='<txp:variable name="archivemonth" />' section="article" />

	</txp:if_variable>

<txp:else />

	<p>No articles were created during this time period.</p>

</txp:if_variable>

4. Create the archives article form:

<a href="<txp:permlink />" title="Permanent link to <txp:title />"><txp:title /></a><br />

5. Change the error_default page to:

<txp:output_form form="header" />

<div id="primary">
	<div id="content" role="main">

		<h1 class="page-title"><txp:error_status /></h1>

		<p class="error-msg"><txp:error_message /></p>

	</div><!-- #content -->
</div><!-- #primary -->

<txp:output_form form="sidebar" />

<txp:output_form form="footer" />

6. One key to making this work is this extremely custom error_404 page:

<txp:output_form form="header-archives" />

<txp:output_form form="header" />

	<div id="primary">
		<div id="content" role="main">

<txp:if_variable name="archivemonth" value="archives">

	<h1 class="page-title"><txp:error_status /></h1>

	<p class="error-msg"><txp:error_message /></p>

<txp:else />

	<txp:output_form form="task-archives" />

</txp:if_variable>

		</div><!-- #content -->
	</div><!-- #primary -->

<txp:output_form form="sidebar" />

<txp:output_form form="footer" />

and the pagetitle code looks like:

<txp:if_section name="default">
	<title><txp:page_title /></title>
<txp:else />
	<txp:smd_if field="txpvar:archivepagetitle" operator="isused">
		<title><txp:variable name="archivepagetitle" /></title>
	<txp:else />
		<title><txp:page_title /></title>
	</txp:smd_if>
</txp:if_section>

You will also need mkp_archive as I am sure you noticed in the header-archives form. You won’t need adi_gps anymore because this will performs the operations I was doing with that along with some reverse engineered Textpattern code.

To see this version in action, try these: Yearly, Monthly, Daily

Last edited by michaelkpate (2012-01-26 21:54:17)


Textpattern Community Member since 2004-02-23 | World Headquarters

Offline

Board footer

Powered by FluxBB