Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Detecting specific article counts
Given this, included on every page, to detect if I’m inside a section that only has one article:
<if::article_list>
<txp:variable name="article_count" trim><items::count text="" /></txp:variable>
</if::article_list>
<!-- Displays [1] -->
[<txp:variable name="article_count" />]
<!-- Displays MULTIPLE -->
<if::variable name="article_count" value="1">SINGLE<txp:else />MULTIPLE</if::variable>
What am I missing? The trim seems to account for getting rid of spaces, so the (default) exact match should work, right?
I need to know in the <head>
so I can output a meta canonical tag to point to the actual article if I’m on a list page with a single article (e.g. /about) to avoid SEO duplicate content warnings.
EDIT: This is on 4.9.0-beta.2. Same behaviour in the archive
Page on the demo site.
EDIT 2: Ah, okay, it works if I directly assign the value="1"
to the variable instead of the <items::count>
tag, so this must be a processing order thing. I presume the item count isn’t actually available when it’s first encountered and is injected in a later sweep, but the <if::variable>
check is executed immediately so it thinks it’s empty? That doesn’t explain why the value of the variable shows up when I display it, immediately before the <if::variable>
test, but maybe that’s injected too during a subsequent parser sweep? If so, how do I get round this? Some sort of [1]
processing order hack?
Last edited by Bloke (2025-05-07 08:26:32)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Detecting specific article counts
Yep, <txp:items_count />
is populated by <txp:article />
, so the latter must be processed first: <txp:article[1] />
.
Bloke wrote #339666:
That doesn’t explain why the value of the variable shows up when I display it, immediately before the
<if::variable>
test, but maybe that’s injected too during a subsequent parser sweep?
Yes, <txp:items_count />
postpones itself to the next pass in this case, so your variable outputs the literal <txp:items_count />
first, which becomes 1
on the second pass.
Offline
Re: Detecting specific article counts
Cool, thanks. I’ll try the article[1]
trick.
This is easy to work around, btw, but it’s a bit of a kludge:
<if::article_list>
<txp:variable name="article_check" trim>
<article::custom section='<txp:section />' limit="2" break="|"><article::id /></article::custom>
</txp:variable>
<txp:variable name="single_article" trim>
<if::variable name="article_check" value="|" match="any">no<txp:else/>yes</if::variable>
</txp:variable>
</if::article_list>
Then I can use <if::variable name="single_article" value="yes"> I'm on a single page</if::variable>
anywhere.
Can anyone think of any less resource-intensive ways to get this info?
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Detecting specific article counts
I can’t remember: does items_count work outside a search context? Otherwise, maybe use the pgonly
attribute? Or the article(_custom) tag with offset="1"
and txp:evaluate. If sql queries are cached (?), that shouldn’t “cost” too much…
TXP Builders – finely-crafted code, design and txp
Offline
Re: Detecting specific article counts
jakob wrote #339669:
I can’t remember: does items_count work outside a search context?
Yes it does. We deprecated <txp:search_results_count>
in favour of it in 4.9.0 because it actually works across the board.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Detecting specific article counts
jakob wrote #339669:
maybe use the
pgonly
attribute? Or the article(_custom) tag withoffset="1"
and txp:evaluate
Nice ideas, thank you. The offset
trick might be less invasive than my initial hack of having a couple of variables floating around and the conditional.
Last edited by Bloke (2025-05-07 09:30:55)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Detecting specific article counts
etc wrote #339667:
the latter must be processed first:
<txp:article[1] />
.
That works beautifully on single pages and multiple pages, thank you. I just added that to my default form and it worked. However, on the frontpage that uses a different page template:
Tag error: <items::count text=”“ /> -> Textpattern Warning: secondpass < 2
I’m not using a <txp:article>
tag on the home page anywhere. It just contains a bunch of <txp:article_custom>
tags to pull in hero articles and the latest N products, into various sections. Do I need to add [1]
to all the article_custom tags too?
EDIT: Nope, adding [1]
to those doesn’t help. Guess I need to also exclude the counter if the section=""
too.
Last edited by Bloke (2025-05-07 09:42:01)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Detecting specific article counts
This kludge works:
<if::section name="">
<txp:variable name="single_article" value="no" />
<txp:else />
<txp:variable name="article_count" trim><items::count text="" /></txp:variable>
<txp:variable name="single_article" trim>
<if::variable name="article_count" value="1">yes<txp:else />no</if::variable>
</txp:variable>
</if::section>
It’s probably more elegant to use <txp:evaluate>
directly on the <items::count>
and save the variable dance, but I always get the test
syntax wrong and it takes me ages to scratch my head over it.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Detecting specific article counts
Lots of roads lead to Rome … :-)
TXP Builders – finely-crafted code, design and txp
Offline