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.
10 Comments - Comments RSS Feed
Marie Poulin
# 19 March 2009
Hello, I tried using this on a page i was working on, but couldn’t quite get it to work how i needed it to… so i tweaked it a bit, and seems to work how i want now. I created a section list that looks like this:
<txp:section_list class=“subnav” wraptag=“ul” break=”“ form=“navbuild” sections=“identity, stationery, invitations, brochures, advertisting, websites, jewelry” active_class=“active” />
then in the form navbuild, i put this:
<!— 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, don’t display the section —>
<txp:else />
<!— check if more than one article in this section —>
<txp:variable name=“morethanone” value=’<txp:article_custom section=’‘<txp:section />’‘ offset=“1” limit=“1” form=“navbuildlink”/>’ />
<txp:if_variable name=“morethanone” value=”“>
<!— only one article in this section, only display linked section page —>
<li<txp:if_section name=’<txp:section />’>
<txp:if_individual_article>class=“active”<txp:else />class=“active”</txp:if_individual_article></txp:if_section>>
<txp:section link=“1” title=“1” /></li>
<txp:else />
<txp:if_different>
<li<txp:if_section name=’<txp:section />’>
<txp:if_individual_article>class=“active”<txp:else />class=“active”</txp:if_individual_article>
</txp:if_section>>
<txp:section link=“1” title=“1” /></li>
</txp:if_different>
<txp:if_section name=’<txp:section />’>
<txp:article_custom section=’<txp:section />’ form=“navbuildlink” wraptag=“ul” class=“subnav” />
</txp:if_section>
</txp:if_article_list>
</txp:if_variable>
</txp:if_variable>
Not sure if i missed something, or if there is an easier way to do this, but you can see an example of this working here: http://sylviegosselin.com/invitations/
Tom Beech
# 20 May 2010
This is great and exactly what I needed for my on page sitemap. Is there any way that this could be altered to include sticky articles as well. At the moment it only seems to display live articles and their sections.
Mischa van der Spek
# 5 October 2010
Hi Els,
As Tom Beech said, dynamic static articles would make it perfect!
At the moment I manually added them by reproducing the ‘Home’ li tag and changing their names…

Els
# 14 February 2009
Just a side note: of course this code doesn’t have to be inside an article, you can just as well put it in the page template.