Textpattern tips, tutorials and code snippets

Customising the next prev links

Using the in-built tags will – in most case – get you where you need to go.

Sometimes however one needs to customise the output somewhat, as in this example, where we attempt to style the HTML produced by the link_to_next tag and the link_to_prev tag .

What the tags do

By default, link_to_next and link_to_prev provide us with (from TextBook):

The link_to_prev tag can be used as a single tag or a container tag to return the permanent URL of the previous article by posting date.
If used as a container tag, the HTML required to output a hyperlink is returned; if used as a single tag, only the URL itself is returned.

The default method

By default, Textpattern uses this method to create a link to the previous and next articles, within an if_individual_article context:

<txp:link_to_prev><txp:prev_title /></txp:link_to_prev> <txp:link_to_next><txp:next_title /></txp:link_to_next>

What we want to do

In this instance, we’d like to customise the output by making the link a nice button. Something like this:

<p><a href="<txp:link_to_prev />" title="<txp:prev_title />" class="button orange">« <txp:prev_title /></a>
<a href="<txp:link_to_next />" title="<txp:next_title />" class="button orange"><txp:next_title /> »</a></p>

The problem…

The above code works fine, with the exception that if we are on the first or last article page the <a> hyperlink tag will display with » inside the tag. Why? <txp:link_to_next><txp:next_title /></txp:link_to_next> or <txp:link_to_prev><txp:prev_title /></txp:link_to_prev> would return nothing if we are on the first or last article page, but since we have placed the tags within our own <a> hyperlinks, an output is generated.

The solution

The solution (provided kindly by the Queen of TXP tags – Els) is the following:

<txp:variable name="prev" value='<txp:link_to_prev />' />
<txp:variable name="next" value='<txp:link_to_next />' />
<p><txp:if_variable name="prev" value="">
<txp:else />
	<a href="<txp:link_to_prev />" title="<txp:prev_title />" class="button orange">« <txp:prev_title /></a>
</txp:if_variable>
<txp:if_variable name="next" value="">
<txp:else />
	<a href="<txp:link_to_next />" title="<txp:next_title />" class="button orange"><txp:next_title /> »</a>
</txp:if_variable></p>

In the example, a variable is to set to the value of <txp:link_to_prev /> and <txp:link_to_next />. If nothing exists, no output will be generated.

Following this, we check if the variable is empty. If it is not empty, we know we are not on either the last or first article page, so please go ahead and display our fancy button links.

Of course, one can also use this method for the <txp:older /> and <txp:newer /> tags for article list pages.

Thanks to Els, as always for coming up with this solution ;-)

2 Comments Comment feed

I am still using the good old ‘before txp:variable’ container tag plug-in chh_if_data which also only outputs something if the wrapped tag returns something.

“Show a block of text only if enclosed Txp tags produce some output.”

You can accomplish a similar effect without variables by containing everything within the <link_to...> tags (thus they won’t display if those tags return nothing) and putting your classes on the enclosing element, like a div:

<div class="prevNext">
<span id="linkToPrev"><txp:link_to_prev>&#8592;&nbsp;<txp:prev_title /></txp:link_to_prev></span>
<span id="linkToNext"><txp:link_to_next><txp:next_title />&nbsp;&#8594;</txp:link_to_next></span>
</div>

Yes, the spans will be output even if they’re not necessary, but if you only style the links themselves, the spans won’t be visible on the page.

Add a comment

Use Textile help to style your comments