Textpattern tips, tutorials and code snippets

List related links from categories and sections

I wanted a list of 10 links in each page, populated with related links (same category and section) and augmented with links from the same section, omiting any duplicate entries.

The solution was proposed by Jakob and Uli.

<txp:variable name="relatedtexts" 
	value='<txp:related_articles limit="10" break=",">
		<txp:article_id />
	</txp:related_articles>'/>
<txp:variable name="moretexts" 
	value='<txp:article_custom 
		limit="10" section=''<txp:section />'' 
		break="," sort="rand()">
	<txp:article_id />
	</txp:article_custom>' />

<txp:if_variable name="relatedtexts" value="">
<txp:article_custom limit="10" section='<txp:section />' break="li" wraptag="ul" sort="rand()" class="related_articles"><txp:permlink><txp:title /></txp:permlink></txp:article_custom>
<txp:else />

<txp:article_custom 
	wraptag="ul" 
	break="li" 
	limit="10" 
	class="related_articles" 
	id='<txp:rah_repeat offset="1" 
	break="," duplicates="1" 
	sort="regular asc" 
		value=''<txp:variable name="relatedtexts"/>,<txp:variable name="moretexts" />''>
		<txp:rah_repeat_value />
	</txp:rah_repeat>' 
		sort='field(ID,<txp:variable name="relatedtexts" />,<txp:variable name="moretexts" />)'>
  <txp:permlink><txp:title /></txp:permlink>
</txp:article_custom>
</txp:if_variable>

The above code produces a linked list:

  • related link 1
  • related link 2
  • related link n
  • same section link n+1
  • same section link n+2
  • same section link 10

For further information, see the related forum thread.

Use Textile help to style your comments