Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
TXP variable and counting
I’ve got a strange behavior with variable.
I need a short code in order to inject only one, JavaScript call:
<txp:variable name="gallery_counter" add />
<div class="image-gallery<txp:variable name="gallery_counter" />">
</div>
<txp:if_variable name="gallery_counter" value="1">
<script>
// some code...
</script>
</txp:if_variable>
The short code works, but… the count starts to… 7. 😱
Sure, I’m using other variables in my page (maybe 6, that’s right).
Question: How can I get a counter starting from 1? (TXP 4.9-dev)
Last edited by Pat64 (2024-01-27 03:50:27)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: TXP variable and counting
Hi Patrick
Where have you initilized the variable?
<txp:variable name="gallery_counter" value="0" />
Offline
Re: TXP variable and counting
Yep. I tried (based on this post, we don’t need to initialize a variable)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: TXP variable and counting
That’s a weird-sounding one. I tried to reproduce it by putting the following
<txp:variable name="demo_counter" add />
<txp:variable name="sample_counter" add />
<txp:variable name="test_counter" add />
<li class="article" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
<h4 itemprop="headline" id="article<txp:variable name="test_counter" />">
<a href="<txp:permlink />" itemprop="url mainEntityOfPage" title="<txp:text item="read_more" />">
<txp:title /> – <txp:variable name="test_counter" /> ( <txp:variable name="demo_counter" /> ) { <txp:variable name="sample_counter" /> }
</a>
</h4>
<txp:if_variable name="test_counter" value="1">
<h3>First counter</h3>
</txp:if_variable>
…
in the article_listing
form of the demo site, but I get the expected output:
Some wild guesses:
- Try giving the counter name an entirely different name. Does it still happen?
- Could the form in which the counter is advanced be being called multiple times?
- Could the counter be advancing for every article but your galleries only appear on some articles, e.g. the first gallery happens to be on the seventh article in the list?
- Try outputting
<txp:php>global $variable; dmp($variable);</txp:php>
at a strategic place on the page to see if that gives you any clues.
TXP Builders – finely-crafted code, design and txp
Offline
Re: TXP variable and counting
Hi Jacob.
Thank you very much for your efforts.
All my variables have unique names.
You are right: the counter takes the article number as its starting point!
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: TXP variable and counting
Pat64 wrote #336478:
You are right: the counter takes the article number as its starting point!
Hehe, I’ve done that before too!!
TXP Builders – finely-crafted code, design and txp
Offline
Re: TXP variable and counting
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Re: TXP variable and counting
Pat64 wrote #336481:
So what is the solution?
Advance the counter only in the cases where you have a gallery. If, for example, you have a gallery when you have more than one article image, then test for offset="1" limit="1"
of your article_image, for example
<txp:evaluate query='<txp:custom_field name="article_image" offset="1" limit="1" />'>
<txp:variable name="gallery_counter" add />
<h5>gallery #<txp:variable name="gallery_counter" /></h5>
<txp:if_variable name="gallery_counter" value="1">
<h6>first counter</h6>
</txp:if_variable>
</txp:evaluate>
Testing on the demo site – with four articles where articles two and four have three and two image ids in the article_image field – I get the following:
TXP Builders – finely-crafted code, design and txp
Offline
Re: TXP variable and counting
Jacob, this is fabulous!
You win (another) gold medal for the best Textpattern user of the day. 🥇
Thank you lot. ;)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
Pages: 1