Textpattern offers an in-built method for paging result sets – page 1, page 2 and so on – via the txp:newer and txp:older tags.
A colleague was working on a site where he wanted the <div id="navigation">
to display only if there was a value for either <txp:older />
or <txp:newer />
.
The first revision
This is the first code revision:
<txp:if_article_list>
<!-- entry navigation -->
<div id="navigation">
<txp:variable name="older" value='<txp:older />' />
<txp:variable name="newer" value='<txp:newer />' />
<txp:if_variable name="newer" value="">
<txp:else />
<div class="nav-links"><txp:newer>Next</txp:newer> →</div>
</txp:if_variable>
<txp:if_variable name="older" value="">
<txp:else />
<div class="nav-links">← <txp:older>Previous</txp:older></div>
</txp:if_variable>
<!-- entry entry navigation -->
</div>
</txp:if_article_list>
Nice, but it does not work because #navigation
is placed outside of the variable call. What we need to do is to place <txp:newer />
and <txp:older />
inside the variable.
The final code
<txp:if_article_list> <txp:variable name="has_np" value='<txp:older /><txp:newer />' />
<txp:if_variable name="has_np" value=""> <txp:else /> <!-- entry navigation --> <div id="navigation"> </txp:if_variable>
<txp:older> <div class="nav-links">→ <a href="<txp:older />"><txp:text item="prev" /></a></div> </txp:older>
<txp:newer> <div class="nav-links"><a href="<txp:newer />"><txp:text item="next" /></a> →</div> </txp:newer>
<txp:if_variable name="has_np" value=""> <txp:else /> <!-- end entry navigation --> </div> </txp:if_variable> </txp:if_article_list>
As you can see, we first check if either newer or older values exist and if so, place them in a variable: <txp:variable name="has_np" value='<txp:older /><txp:newer />' />
. If the variable exists, <div id="navigation">
is displayed.
Great!
Just this morning I tried to apply this tip!
Unfortunately without success.
Thanks Stef
Hi friends of TXP Tips,
I think I’ve spotted an error here. Nesting <txp:older /> (or <txp:newer />) inside a <txp:older></txp:older> (or <txp:newer></txp:newer>) will give invalid markup, as the container creates a link (<a>) around the contained code, which already has a link.
Also, if I’m not wrong, the “duplicated” if_variable for creating opening and closing div tags for #navigation could be converted in just one if_variable at top (well, just below the line were the variable has been created) that will output just all or nothing.
I’ll check on this Maniqui – in the meantime have any suggestions for changing the code?
Does this work better?:-
though you can still end up with an empty “nav-links” div if one or other of the Txp tags is empty.
So that was the short version. If my memory serves me right you can keep resetting the variable so maybe this would remove any empty divs:-
and yes, I probably should have come up with that the first time but it is much easier to see when the code is there in front of you. ;)
Nice code Stuart! Stef came up with this code for me on a site I am working on. This is the final revision which works ok:
Both of those make sense, basically what Maniqui was suggesting. Maybe worth updating the original post after contacting Stef.