Create an archive or sitemap page by section

The following code was inspired by this comment on Kevin Potts’ tip about a navigation system, but I am using it myself on a sitemap page – inside an article.

The objective

  • Create an archive page by section
  • Don’t display sections if there are no articles
  • If a section contains only one article, don’t display the article but link to the section page

Place the code in your article

<ul>
	<li><txp:link_to_home>Home</txp:link_to_home></li>
	<txp:section_list sort="title" exclude="sections,not,to,be,displayed" break="">
		<!-- check if there are any articles in this section -->
		<txp:variable name="hasarticle" value='<txp:article_custom section=''<txp:section />'' limit="1" />' />
		<txp:if_variable name="hasarticle" value="">
			<!-- no articles in this section, don't display the section -->
		<txp:else />
			<!-- check if there is more than one article in this section -->
			<txp:variable name="morethanone" value='<txp:article_custom section=''<txp:section />'' offset="1" limit="1" />' />
			<txp:if_variable name="morethanone" value="">
				<!-- only one article in this section, only display linked section page -->
				<li><txp:section title="1" link="1" /></li>
			<txp:else />
				<!-- more than one article in this section, display linked section page and articles -->
				<li><txp:section title="1" link="1" />
				<txp:article_custom section='<txp:section />' limit="999" sort="title asc">
					<txp:if_first_article><ul></txp:if_first_article>
					<li><a href="<txp:permlink />" title="<txp:title />"><txp:title /></a></li>
					<txp:if_last_article></ul></txp:if_last_article>
				</txp:article_custom>
				</li>
			</txp:if_variable>
		</txp:if_variable>
	</txp:section_list>
</ul>

The above code is heavily commented so should be self-explanatory. Place it in your article and assign it to either your archive or sitemap section.

Note: When using this code in an article like in the Archive page in an article tip, you must specify a form in the article_custom call (inside the txp:variable tag) that is different from the form the article itself is called by, to avoid a circular reference.

You can see the results of the code on this sitemap.

comments powered by Disqus