Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
[howto] Hand-made pagination with article_custom
This is the best way I’ve been able to come up with to enable pagination with article_custom article lists.
Required plugins: adi_gps, adi_calc, and rah_repeat.
<txp:adi_gps name="page" quiet="1" />
<txp:variable name="article-count" value="0" />
<txp:variable name="article-limit" value="10" /> <!--Set this to whatever you want-->
<txp:if_variable name="page" value=""><txp:variable name="page" value="1" /></txp:if_variable>
<txp:adi_calc name="offset" value='<txp:variable name="page" />' subtract="1" />
<txp:adi_calc name="offset" multiply='<txp:variable name="article-limit" />' />
Duplicate the article_custom tag that you are trying to paginate, paste it before the “real” one, and set limit="9999"
. Make it a container tag with just <txp:adi_calc name="article-count" add="1" />
in it.
Right after that article_custom tag, add this line: <txp:adi_calc name="page-count" value='<txp:variable name="article-count" />' div='<txp:variable name="article-limit" />' ceiling="1" />
.
Next, in the “real” article_custom tag that you want to paginate, set limit='<txp:variable name="article-limit" />'
and offset='<txp:variable name="offset" />''
.
Page Navigation Option #1: Google list of links
<ul class="pagination">
<txp:rah_repeat limit='<txp:variable name="page-count" />' value="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20" >
<li>
<txp:if_variable name="page" value='<txp:rah_repeat_value />' >
<txp:rah_repeat_value />
<txp:else />
<a href="?page=<txp:rah_repeat_value />" ><txp:rah_repeat_value /></a>
</txp:if_variable>
</li>
</txp:rah_repeat>
</ul>
If you have other url variables that you are using to filter articles lists using adi_gps, you can preserve these between pages by adding the necessary markup to the link. For example, the following line might be what you want if you are filtering by section, category, and a couple url variables (usually custom fields):
<a href="?page=<txp:rah_repeat_value />&s=<txp:section />&c=<txp:category />&urlvar1=<txp:variable name="urlvar1" />&urlvar2=<txp:variable name="urlvar2" />" ><txp:rah_repeat_value /></a>
Page Navigation Option #2: Drop-down page select
<form action='<txp:page_url type="request_uri" />' method="get">
<select name="page" onChange="this.form.submit();">
<txp:rah_repeat limit='<txp:variable name="page-count" />' value="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20" >
<txp:if_variable name="page" value='<txp:rah_repeat_value />' >
<option selected value='<txp:rah_repeat_value />'><txp:rah_repeat_value /></option>
<txp:else />
<option value='<txp:rah_repeat_value />'><txp:rah_repeat_value /></option>
</txp:if_variable>
</txp:rah_repeat>
</select>
<noscript><input type="submit" value="Go!"></noscript>
</form>
If you have other url variables that you are using to filter articles lists using adi_gps, you can preserve these between pages by adding extra select elements before the closing </form>
tag. You can hide these extra select elements from being displayed with a simple .hidden { display: none; }
CSS rule. Here is an example:
<select class="hidden" name="urlvar1">
<txp:if_variable name="urlvar1" value="">
<option selected value=""></option>
<txp:else /><option selected value='<txp:variable name="urlvar1" />'></option>
</txp:if_variable>
</select>
Last edited by colak (2012-03-25 09:39:33)
Offline
Re: [howto] Hand-made pagination with article_custom
Posted on TXP Tips – thanks Andrew for the excellent tutorial!
Offline
Re: [howto] Hand-made pagination with article_custom
Thanks Aswihart for your soution, that saved my day yesterday as no plugin I could use was paginating a special article_custom generated list.
I made some modification to the final part of the code (the pagination list). I just added prev/next links at start and finish of the pagination list, as well as some comma thrown in as separators. Maybe that could be useful to someone, so I post it here.
Note that this assumes that you already have the first part of the code posted above by Aswihart, and you only need to replace the <ul class="pagination">
til </ul>
part.
<txp:if_variable name="page-count" value="1">
<txp:else/>
<txp:adi_calc name="previous" value='<txp:variable name="page" />' subtract='1' />
<txp:adi_calc name="following" value='<txp:variable name="page" />' add="1" />
<ul class="pagination">
<txp:if_variable name="previous" value="0"><li> Pages: </li><txp:else/>
<li><a href="?page=<txp:variable name="previous" />" >« Previous</a></li><li>Pages: </li></txp:if_variable>
<txp:rah_repeat limit='<txp:variable name="page-count" />' value="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20" >
<li><txp:rah_repeat_if_first><txp:else/>, </txp:rah_repeat_if_first><txp:if_variable name="page" value='<txp:rah_repeat_value />' >
<span><txp:rah_repeat_value /></span>
<txp:else />
<a href="?page=<txp:rah_repeat_value />" ><txp:rah_repeat_value /></a>
</txp:if_variable></li>
</txp:rah_repeat>
<txp:if_variable name="page" value='<txp:variable name="page-count" />' >
<txp:else/>
<li><a href="?page=<txp:variable name="following" />" > Next » </a></li>
</txp:if_variable>
</ul>
</txp:if_variable>
Last edited by Zanza (2012-08-03 13:33:35)
Offline
Re: [howto] Hand-made pagination with article_custom
I always like to place article in an Article section and then have a separate Archives pages. I noticed doing that totally broke the Pagination links in Phil’s default templates but after doing some research this was exactly what I needed to fix it.
1) All my articles are in Article and my archives section will be called Archives
2) Make a copy of the archive page and set Article and Archives to different copies.
3) Go to your Archives page and replace <!— Main content —> with the code below.
<!-- Main content -->
<main aria-label="<txp:text item="main_content" />" itemscope itemtype="http://schema.org/Blog">
<h1 itemprop="name">
<txp:section title="1" />
</h1>
<txp:adi_gps name="page" quiet="1" />
<txp:variable name="article-count" value="0" />
<txp:variable name="article-limit" value="5" /> <!--Set this to whatever you want-->
<txp:if_variable name="page" value=""><txp:variable name="page" value="1" /></txp:if_variable>
<txp:adi_calc name="offset" value='<txp:variable name="page" />' subtract="1" />
<txp:adi_calc name="offset" multiply='<txp:variable name="article-limit" />' />
<txp:article_custom section="article" limit="9999">
<txp:adi_calc name="article-count" add="1" />
</txp:article_custom>
<txp:adi_calc name="page-count" value='<txp:variable name="article-count" />' div='<txp:variable name="article-limit" />' ceiling="1" />.
<txp:article_custom section="article" class="article-list" form="article_listing" wraptag="ul" limit='<txp:variable name="article-limit" />' offset='<txp:variable name="offset" />' />
<txp:adi_calc name="prev" value='<txp:variable name="page" />' subtract="1" />
<txp:adi_calc name="next" value='<txp:variable name="page" />' add="1" />
<p class="paginator">
<txp:rah_repeat limit='<txp:variable name="page-count" />' range="1, 9999" >
<txp:if_variable name="prev" value='<txp:rah_repeat_value />'>
<a rel="prev" href="?page=<txp:rah_repeat_value />">
<txp:text item="newer" />
</a>
</txp:if_variable>
<txp:if_variable name="next" value='<txp:rah_repeat_value />'>
<a rel="next" href="?page=<txp:rah_repeat_value />">
<txp:text item="older" />
</a>
</txp:if_variable>
</txp:rah_repeat>
</p>
</main>
4) You should now have Newer and Older links at the bottom. To test it, you can change the article-limit to a lower number.
Offline