Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2025-06-02 07:03:42

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,659
GitHub Twitter

Add up the number of calls to a short code.

How, and is it possible, to total the number of calls of a Short Code?

I made a testimonials model in pure CSS, and I need to know how many times the user invokes it in order to insert some corresponding <input> tags as selectors.

<txp::quote lang="en" text="Blockquote text..." author="Author's name" />

Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#2 2025-06-02 07:23:45

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,925
Website GitHub

Re: Add up the number of calls to a short code.

Maybe set a counter variable in the shortcode:

<txp:variable name="testimonials_count" add="1" />

That should count how many times the shortcode is called on one page.


TXP Builders – finely-crafted code, design and txp

Offline

#3 2025-06-02 07:46:58

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,659
GitHub Twitter

Re: Add up the number of calls to a short code.

… Unfortunately, this variable iterates each calls but do not add the sum…

(Tested with TXP 4.8.8)


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#4 2025-06-02 08:17:02

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,925
Website GitHub

Re: Add up the number of calls to a short code.

Pat64 wrote #339765:

… Unfortunately, this variable iterates each calls but do not add the sum…

(Tested with TXP 4.8.8)

Maybe I misunderstood what you want, but with the add="1" attribute it should increment the counter with each call.

I just tried it on the txp 4.8.8. demo page (if you look soon, it might still be there gone). This is the counter shortcode form:

<p>Some test output per shortcode</p>
<txp:variable name="testimonials_count" add="1" />

And I added

<txp::counter />

to the default and article_listing forms under the heading.

And then in the body_footer form there is:

Counter total: <txp:variable name="testimonials_count" output />

After adding some dummy articles, the footer shows the total number of times the shortcode was called, e.g. 6 at present.


TXP Builders – finely-crafted code, design and txp

Offline

#5 2025-06-02 09:22:44

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,659
GitHub Twitter

Re: Add up the number of calls to a short code.

Sure, Julian.

Because you call the result of the variable within another form. I suspect it’s not possible into the same form…


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#6 2025-06-02 10:00:47

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,925
Website GitHub

Re: Add up the number of calls to a short code.

Does this help?

<txp:variable name="testimonials_count" add="1" output />

I just reimplemented that on the demo site and it then shows a running cumulatively incrementing value with each instance of the shortcode.

EDIT: BTW, this does also work:

<p>Some test output per shortcode</p>
<txp:variable name="testimonials_count" add="1" output />

<!-- counter value: <txp:variable name="testimonials_count" /> -->

if you want to use the current value in something else like a class name or something.

EDIT II: Or are you trying to get a {current} of {total} output?

(I played around a bit with [1] notation or process="2" but couldn’t find a working combo. Another way of pre-processing is to output the result of the shortcode to a variable that you then use later on, or in combination with another shortcode, but I couldn’t think of a good way off the top of my head)


TXP Builders – finely-crafted code, design and txp

Offline

#7 2025-06-02 10:41:25

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,659
GitHub Twitter

Re: Add up the number of calls to a short code.

Interesting. Thanks, Julian. I will try ;)


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

Board footer

Powered by FluxBB