Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
List of all article list page urls
How can I get a list of all article list page urls for a particular section starting from page 2? I’m needing this for the sitemap.
I’m looking for something like:
<txp:variable name="siteurl" />section_name/?pg=2
<txp:variable name="siteurl" />section_name/?pg=3
<txp:variable name="siteurl" />section_name/?pg=4
...
<txp:variable name="siteurl" />section_name/?pg=x
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: List of all article list page urls
OK, I got this with php, but I was wondering if it could be done with tags οr without having to specify the $endPage
.
<txp:php>
$siteUrl = 'https://www.site.tld';
$section = 'section_name';
$startPage = 2;
$endPage = 100;
for ($i = $startPage; $i <= $endPage; $i++) {
echo "<url>";
echo "<loc>{$siteUrl}/{$section}/?pg={$i}</loc>";
echo "<changefreq>monthly</changefreq>";
echo "</url>";
}
</txp:php>
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: List of all article list page urls
You can calculate and plug endPage
into <txp:php />
:
<txp:php endPage='<txp:article_custom section="section_name" pageby="10" pgonly />'>
...
</txp:php>
There is also <txp:pages />
solution (instead of <txp:php />
), but I don’t remember howto.
Offline
Re: List of all article list page urls
etc wrote #340753:
You can calculate and plug
endPage
into<txp:php />
:
Thanks so much Oleg. It might take me a few days as, as you know my php skills are rudimentary.
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: List of all article list page urls
This is what I have tried:
<txp:variable name="latest_posted_article"><txp:article_custom section="my_section" limit="1" exclude="2240,2241" sort="posted desc" status="live"><txp:modified format="w3c" /></txp:article_custom></txp:variable>
<txp:variable name="page_count"><txp:evaluate query='<txp:article_custom section="my_section" pageby="10" pgonly /> - 1' /></txp:variable>
<txp:php>
$siteUrl = 'https://www.site.tld';
$section = 'my_section';
$startPage = 2;
$pagesNumber = "<txp:variable name='page_count' />";
$endPage = $pagesNumber;
$latestModified = '<txp:variable name="latest_posted_article" />';
for ($i = $startPage; $i <= $endPage; $i++) {
echo "<url>";
echo "<loc>{$siteUrl}/{$section}/?pg={$i}</loc>";
echo "<lastmod>{$latestModified}</lastmod>";
echo "</url>";
}
</txp:php>
but I’m getting an out of memory error because of the evaluate
tag. The sitemap as it is currently generated can be found on github.
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: List of all article list page urls
Oleg’s example shows you how to pass the results of a parsed tag to a txp:php block as a ready-to-use php variable, so you should be able to do:
<txp:php endPage='<txp:article_custom section="section_name" pageby="10" pgonly />'
latestModified='<txp:article_custom section="section_name" limit="1" exclude="2240,2241" sort="posted desc" status="live"><txp:modified format="w3c" /></txp:article_custom>'>
$siteUrl = 'https://www.site.tld';
$section = 'my_section';
for ($i = 1; $i <= ($endPage - 1); $i++) {
echo "<url>";
echo "<loc>{$siteUrl}/{$section}/?pg=" . ($i + 1) . "</loc>";
echo "<lastmod>{$latestModified}</lastmod>";
echo "</url>";
}
</txp:php>
So if endPage
returns 10 pages, it will loop over $i = 1 to 9, but output pg=2 to 10.
TXP Builders – finely-crafted code, design and txp
Offline
Re: List of all article list page urls
Wondering aloud, can you loop over a from-to range using a core tag? I know that’s possible with rah_repeat and maybe smd_each. If so, you wouldn’t need the php block at all.
TXP Builders – finely-crafted code, design and txp
Offline
Re: List of all article list page urls
More thinking aloud. Would this alternative approach work, which uses txp:variable to loop over a comma-separated list?
<txp:variable name="lastmodified_article"><txp:article_custom section="section_name" limit="1" exclude="2240,2241" sort="posted desc" status="live"><txp:modified format="w3c" /></txp:article_custom></txp:variable>
<txp:php variable="page_nums" page_count='<txp:article_custom section="articles" pageby="5" pgonly />'>
// make a string: 2,3,4,5,6,… and save in a variable
$numbers = range(2, $page_count);
echo implode(',', $numbers);
</txp:php>
<txp:variable name="page_nums" breakby output>
<url>
<loc><txp:site_url />section_name/?pg=<txp:yield item /></loc>
<lastmod><txp:variable name="lastmodified_article" /></lastmod>
</url>
</txp:variable>
I’m not sure that txp:variable containers can produce output like that?
EDIT: It seems not, however you can put the output into a separate form and then specify that as the breakform
, e.g.
<txp:variable name="page_nums" breakby breakform="sitemap_item" output />
and the breakform sitemap_item
(of type misc):
<url>
<loc><txp:site_url />section_name/?pg=<txp:yield item /></loc>
<lastmod><txp:variable name="lastmodified_article" /></lastmod>
</url>
TXP Builders – finely-crafted code, design and txp
Offline
Re: List of all article list page urls
Amazing that breakform
works (?) here, it turns <txp:variable />
into universal iterator. You can also use <txp:pages />
for numeric ranges:
<txp:pages total='<txp:article_custom section="articles" pageby="5" pgonly />' break="url" link="">
<txp:yield item="page" /><!-- add extra markup here -->
</txp:pages>
Offline
Re: List of all article list page urls
jakob wrote #340768:
EDIT: It seems not, however you can put the output into a separate form and then specify that as thebreakform
, e.g.
<txp:variable name="page_nums" breakby breakform="sitemap_item" output />...
and the breakform
sitemap_item
(of type misc):
<url>...
My next test:)
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Re: List of all article list page urls
etc wrote #340771:
You can also use
<txp:pages />
for numeric ranges:
As ‘entertaining’ as my detour using txp:variable to loop over items was, Oleg’s solution is more elegant and succinct (as one would expect ;-):
<txp:variable name="lastmodified_article"><txp:article_custom section="section_name" limit="1" sort="posted desc" status="live"><txp:modified format="w3c" /></txp:article_custom></txp:variable>
<txp:pages total='<txp:article_custom section="section_name" pageby="10" pgonly />' break="url" link="" offset="1">
<loc><txp:site_url />section_name/?pg=<txp:yield item="page" /></loc>
<lastmod><txp:variable name="lastmodified_article" /></lastmod>
</txp:pages>
I just tried it out on the dev demo site (should be online for another 40 minutes after this post) and it works great! (add offset="1"
to Oleg’s code to start with pg=2).
TXP Builders – finely-crafted code, design and txp
Offline
Re: List of all article list page urls
Thanks so much to both of you. That works as intended and there is no problem with memory usage either!!!
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline