Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2011-11-16 16:28:42

davidpmccormick
New Member
Registered: 2011-11-15
Posts: 9

Display 'article x of y' on individual article pages

Hi,

I want to display something like, ‘article 3 of 10’ on my individual article pages for articles of a specific section, ordered by date published.

Using the mdn_count plugin, I can get the total number, but I can’t figure out a way to get the current number without being in article list view. Is there a way to do this (bearing in mind I’m very new to Textpattern).

Thanks in advance,
David

Offline

#2 2011-11-16 17:31:23

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,315

Re: Display 'article x of y' on individual article pages

Unless someone with cool PHP/SQL capabilites offers you help, look what soo_multidoc can do.


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#3 2011-11-16 17:58:26

davidpmccormick
New Member
Registered: 2011-11-15
Posts: 9

Re: Display 'article x of y' on individual article pages

Thanks, uli; I gave that a shot, but it’s quite fiddly. I suppose the short answer is there isn’t a simple way to do it.

Offline

#4 2011-11-16 18:08:04

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,315

Re: Display 'article x of y' on individual article pages

If simple means «Drop in tag and lay back» I presume the answer is “wait for someone with said capabilities”.
Though you’re knew to TXP: Something tells me you’ve heard of custom fields but want a not-that-manual way of numbering articles, right?

Last edited by uli (2011-11-16 18:14:21)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#5 2011-11-16 18:11:03

davidpmccormick
New Member
Registered: 2011-11-15
Posts: 9

Re: Display 'article x of y' on individual article pages

That’s right; I’m not averse to custom fields per-se, but if I do it manually then choose to delete an article later on, the numbering will no longer make sense (I think).

I’ve come from Expression Engine, where I was spoiled with {count} and {absolute_count} tags.

I’m sure it will be a good opportunity to brush up on some php!

Offline

#6 2011-11-16 19:10:42

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: Display 'article x of y' on individual article pages

This sounds like a task for smd_query.
You will certainly have some {great} {fun} {unleashing} its {powers}.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#7 2011-11-16 19:13:51

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,315

Re: Display 'article x of y' on individual article pages

Exciting topic! I like the task.
I think there should be a way to make it work with only core tags and one mini plugin.

Install rvm_counter and replace its content with this code. It’s actually a version of the plugin that doesn’t write to the database but does its magic in the server’s memory.

Then put its tag in an article_custom tag with exactly the attributes and values you use to display the article, e.g. restricted to a category like here:

<txp:article_custom category="your-category" sort="posted asc" limit="9999">
	<txp:variable name='art<txp:article_id />' value='<txp:rvm_counter name="art_num" />' />
</txp:article_custom>

It’s just for building the counter values, there’s no visible content. With this tag I create as much variables as there are articles in this category. Each variable has the name of the article ID and contains the counter value. (Note the single quotes I used around tags inside tag’s attribute values. I’m using the container form of the tag as its output won’t be used a second time.)

Deeper down the page I use a second article tag for creating the real, visible, content and I give it the same attributions as the first article tag had:

<txp:article category="your-category" sort="posted asc" limit="9999">
	<txp:title />
	<txp:body />
	Article <txp:variable name='art<txp:article_id />' /> of <txp:mdn_count OPTIONAL ATTRIBUTES />		
</txp:article>

After displaying the text contents it calls in the variable that is named like the ID of the article currently viewed and – hopefully – contains the article’s position in the article chain.

I think you have to initialise the counter by putting this tag on top of the page (and hiding it via CSS): <txp:rvm_counter name="art_num" reset="0" />. Maybe reset has to be 1 instead of 0. Maybe there are even trickier solutions like initialising it inside an (already invisible) variable.

Totally untested and off the top of my head, but something around this should work.

Edited to add some of jakob’s suggestion below. I recommend to also use his replacement for the mdn_counter tag!

Last edited by uli (2011-11-17 01:26:03)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#8 2011-11-16 20:40:58

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,001
Website GitHub

Re: Display 'article x of y' on individual article pages

clever thinking Uli! The second instance needs to be txp:article if it is for an individual article page but the same principle applies of matching the saved variable against the current article id. If you just need to number on the list page, you can the numbering and article list in the same article_custom tag. It’s worth noting that the “y” in the “x of y” is very dependent on having fixed article listing criteria on your list page, i.e. the attributes used for your article_custom tag at the top must match the attributes used on the article list page.

It gets more difficult if you have additional filters on your article list page. For example, if you can look at just one category (= a subset of all the articles), the “y” value will be different, and you’ll need to save the search criteria somehow (cookie? urlvar?) in order to re-use that at the top of the individual article page. Not sure I explained that very well. An example: You have 20 articles altogether, 7 of which are in category A. If you look at that category on the landing page, then decide to click on the fourth article, one would expect to see 4 of 7 on the individual_article page, not 4 of 20 …

EDIT: on second thoughts if one’s going to go to the trouble of saving listing criteria in a cookie, one might as well save the “article number” of the item clicked on on the list page in a cookie, then display that.

BTW: with your idea you should be able to dispense with mdn_count. In your first instance of article_custom at the top make a variable that records the last value of the incrementing counter for the last article, e.g.:

<txp:article_custom category="your-category" sort="posted asc" limit="999">
	<txp:variable name='art<txp:article_id />' value='<txp:rvm_counter name="art_num" />' />
	<txp:if_last_article>
		<txp:variable name="art_total" value='<txp:rvm_counter name="art_num" />' />
	</txp:if_last_article>
</txp:article_custom>

and then in your article form/container use:

<txp:variable name='art<txp:article_id />' /> of <txp:variable name="art_total" />

I’d also add a higher value for limit otherwise it stops counting after the default value of 10 articles.

Theoretically your approach would also work with adi_calc (or a snippet of php) to increment your counter. Many roads and all that…

Last edited by jakob (2011-11-16 20:47:29)


TXP Builders – finely-crafted code, design and txp

Offline

#9 2011-11-16 21:00:25

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,315

Re: Display 'article x of y' on individual article pages

jakob wrote:

clever thinking Uli!

Thanks, jakob, but this is your clever thinking, recognize it? It’s actually a similar (ab/re)use of a tag as a repeater, that you’ve showed me with a section_list some months ago.

Last edited by uli (2011-11-16 21:46:07)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#10 2011-11-16 21:58:02

davidpmccormick
New Member
Registered: 2011-11-15
Posts: 9

Re: Display 'article x of y' on individual article pages

Thanks so much – exactly what I was after (and after a little bit of head-scratching, I believe I understand what’s going on, which is a bonus).

Excuse my ignorance, but am I right in assuming I should remove the ‘c..’ from the start of the code you pasted ? If I don’t, I get a parse error.

Also, using the same system to count the total number, I end up with a number one larger than the correct total; is this to do with the incrementing loop going round once more after the end? Any help on how to solve it would be appreciated (or I can stick with mdn_count, which is what I’ve done for now).

Cheers.

Offline

#11 2011-11-16 22:10:19

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,001
Website GitHub

Re: Display 'article x of y' on individual article pages

recognize it?

You expect me to remember something like that?? Glad we are standing on each other’s shoulders :-)

Also, using the same system to count the total number, I end up with a number one larger than the correct total

ah, rvm_counter is adding another count to it (sorry, not so familiar with that plugin). How about this untested construction?

<txp:article_custom category="your-category" sort="posted asc" limit="999">
	<txp:variable name='art<txp:article_id />' value='<txp:rvm_counter name="art_num" />' />
	<txp:if_last_article>
		<txp:variable name="art_total" value='<txp:variable name=''art<txp:article_id />'' />' />
	</txp:if_last_article>
</txp:article_custom>

not very elegant with those double single quotes I’m afraid, but it’s a tag in a tag in a tag… in other words it uses the value of the variable just created. fingers crossed that it works!

PS: I can’t help thinking there might be some simpler mySQL query to do this without the loop. There’s MySQL SELECT COUNT(*) for getting the total (and a corresponding internal txp function). Maybe there’s a simpler way of getting the row number that matches an id from a search set? That’s beyond me, though.


TXP Builders – finely-crafted code, design and txp

Offline

#12 2011-11-17 01:24:08

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: Display 'article x of y' on individual article pages

Will someone kindly write this up for TXP Tips please (and any other similar awesome tips)…

Offline

Board footer

Powered by FluxBB