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.
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.
Els, I thought I had mentioned that the code could be placed in a page template, but I placed that comment in the next TXP Tip instead – mea culpa! ;-)
Great tip!, I was about to start working on an Archive page when I found this. Thnks!
Nice use of variables. Trying to wrap my head around the logic you wrote, but I’ll try it eventually.
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:
then in the form navbuild, i put this:
Not sure if i missed something, or if there is an easier way to do this.
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.
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…
Of course using an
a href
-tag to show the static articles.Tom and Mischa, sorry, only just noticed your comments. I’ll see if I can post some code for including sticky articles soon.
All, there is a link to my sitemap page in this article. Sorry, it doesn’t exist anymore. Another sitemap using this code can be found here.
Els – I fixed the link to the sitemap in this article … thanks!