Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#286 2014-10-09 20:31:43
Re: smd_tags: unlimited article, image, file and link taxonomy
jakob wrote #284618:
Is there a way of getting the total count of articles output by a
smd_related_tagsquery?
There wasn’t, but try the latest bleeding edge version and let me know how you get on. If you add <txp:smd_tag_count /> after your call to <txp:smd_related_tags /> has completed, it’ll now return the total number of matches it found. It takes into account AND/OR tag sets in the URL.
It’s not exactly had much testing yet, but worked in the limited set of cases I threw at it. Please report back on its success or otherwise and we’ll go from there. I may be able to extend this to do gross counts of articles from other places (like smd_tag_lists) so if that’d be useful I can look into it.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
#287 2014-10-09 21:50:21
Re: smd_tags: unlimited article, image, file and link taxonomy
I miss this place: it’s just great how you people keep coming up with good ideas so quickly!
Thanks, Oleg, for your ideas. That confirms what I thought.
Stef, it does exactly as you say (my emphasis):
If you add <txp:smd_tag_count /> after your call to <txp:smd_related_tags /> has completed, it’ll now return the total number of matches it found. It takes into account AND/OR tag sets in the URL.
But … it still doesn’t do quite what I want, as I have the offset and limit attributes set to achieve pagination with smd_related_tags.
<txp:smd_related_tags
type="article"
section="projects"
limit='<txp:variable name="pageby_projects" />'
offset='<txp:etc_offset pgcounter="page" pageby=''<txp:variable name="pageby_projects" />'' />'
break=""
form="projects_grid-item" />
where “pageby_projects” is just a variable I have set in adi_variables to make it easier to play with settings (apologies for the particularly convoluted example with tag in tag in tag). etc_offset does the nice calculations to output the right articles for each page. As such the tag only outputs a bunch of 9 articles (or whatever the variable is set to) or less.
What I’m trying to get is the total number of matches for tag filter, so that etc_pagination can work its magic correctly. The etc_pagination tags do that nicely for article_custom but not for smd_related_tags (as far as I am aware), so I do that with this snippet instead:
<txp:php>
global $variable;
$variable['num_pages'] = ceil($variable['num_tagged_articles'] / $variable['pageby_projects']);
</txp:php>
where num_tagged_articles is just the tag count without formatting (i.e. <txp:smd_tag_count wrapcount="" class="" />).
BTW: Do you have a text pack for smd_tags somewhere? The bleeding edge version reverts to the placeholders.
TXP Builders – finely-crafted code, design and txp
Offline
#288 2014-10-09 22:59:11
Re: smd_tags: unlimited article, image, file and link taxonomy
jakob wrote #284657:
What I’m trying to get is the total number of matches for tag filter, so that etc_pagination can work its magic correctly.
I haven’t used etc_pagination (shock, horror) so I don’t know its capabilities, nor what it needs as input. I figure it needs the usual three things to calculate how many pages to use:
- The total number of results
- The desired number of records per page
- The current page (offset)
So if the result of searching for a couple of tags returned 30 articles from smd_related tags, plugging that value into etc_pagination’s “limit” and telling it you’d like 10 per page would allow it to figure out that it needs to display 3 pages of stuff. Right? Then it’s a case of getting at etc_pagination’s current page to shove into the offset/limit of smd_related_tags. [I’m modelling that behavior on my pagination patch which never saw the light of day (yet…) but looking at Oleg’s examples, his plugin seems to do things rather differently, so maybe I’m wrong here].
Either way, I think I see the problem. When you use the offset/limit attributes, the total that smd_related_tags spits out at the end is the total number of things matched, with those attributes in place. What you need (I think) is the total number that match without any limit/offset filtering. In other words, the total value should be static for every page load, for every given tag combo, irrespective of which page you’re currently viewing. Is that what you’re after?
Txp does such internal pagination using two queries. First with the criteria applied, but with no limits, the second time with limits. It uses the first for pagination totals and the second for actually displaying results. That’s pretty inefficient and given the heavy nature of smd_tags (even with aggressive cacheing), running the same query twice just for the sake of getting totals is something I’d rather avoid.
MySQL does have a nifty feature SQL_CALC_FOUND_ROWS. Simply adding that to the query and then calling SELECT FOUND_ROWS() immediately afterwards tells you how many rows are in your total result set, regardless of offset/limit. It’s far more efficient than calling the main query twice. Unfortunately (I believe) it’s non-standard SQL so if we move to PDO or something, that’ll not be an option. But I might look into SQL_CALC_FOUND_ROWS in the short term for the plugin, and worry about what the hell to do when we move to PDO on another day!
EDIT: of course, there’s a gotcha. The query returns multiple rows per tag and the PHP filters out duplicates, so maybe a pure MySQL solution isn’t possible unless I manage to rewrite the queries better.
BTW: Do you have a text pack for smd_tags somewhere? The bleeding edge version reverts to the placeholders.
Oh yeah, sorry. I took out the local gTxt() in this version. The top of the plugin template houses the Textpack. Just copy out the strings there and paste the block into the bottom of your Admin->Prefs->Languages panel to install them.
Last edited by Bloke (2014-10-09 23:13:09)
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
#289 2014-10-09 23:33:09
Re: smd_tags: unlimited article, image, file and link taxonomy
Bloke wrote #284661:
SQL_CALC_FOUND_ROWS…
Try the latest version and see if that’s any more useful. If I’ve totally misunderstood your intentions and requirements, please explain it to me like I’m a parsnip and I might then get it.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
#290 2014-10-10 00:32:08
Re: smd_tags: unlimited article, image, file and link taxonomy
Excellent! Just given it a quick run-through and that seems to have done it. Will check again in the morning when I’m more alert, but I think we have a winner! Thank you!
TXP Builders – finely-crafted code, design and txp
Offline
#291 2014-10-10 08:00:57
Re: smd_tags: unlimited article, image, file and link taxonomy
Bloke wrote #284661:
MySQL does have a nifty feature
SQL_CALC_FOUND_ROWS. Simply adding that to the query and then callingSELECT FOUND_ROWS()immediately afterwards tells you how many rows are in your total result set, regardless of offset/limit.
Tremendously useful info, thanks, Stef! Gonna try it somewhere…
Offline
#292 2014-10-10 08:13:18
Re: smd_tags: unlimited article, image, file and link taxonomy
etc wrote #284690:
Tremendously useful info, thanks, Stef! Gonna try it somewhere…
You’re welcome. It’s relatively new to me too but it’s very handy.
To improve things even further (and I may do this in smd_tags if jakob verifies that the count is correct under all conditions he can test), you can cache the total rows after the first query has run — maybe in the session against an md5 of the query string, minus its limit/offset. Subsequent calls for exactly the same query can then take advantage of not having to add SQL_CALC_FOUND_ROWS to the query at all, thus improving performance. You have to be careful if things are changing rapidly in the database so as not to return ‘stale’ or misleading info and flush the value accordingly. The comments on this post offer more.
As I mentioned, the downside is lack of compatibility outside MySQL (as far as I’m aware). Ideas welcome on how to handle situations for other engines under PDO.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
#293 2014-11-27 08:29:57
- saccade
- Plugin Author

- From: Neubeuern, Germany
- Registered: 2004-11-05
- Posts: 521
Re: smd_tags: unlimited article, image, file and link taxonomy
Hi,
I think I have the same problem as kees-b in August:
I have a full list of tags on my site,
when clicking one of them a new page with the related articles opens,
there I still want to have the full list of tags,
but with the one now active highlighted.
Couldn’t find any hint how to find and style the active tag with an if_statement.
Any solution?
Offline
#294 2014-11-27 09:19:52
- saccade
- Plugin Author

- From: Neubeuern, Germany
- Registered: 2004-11-05
- Posts: 521
Re: smd_tags: unlimited article, image, file and link taxonomy
Maybe this at least is a workaround:
<txp:smd_tag_list wraptag="" break="">
<txp:variable name="current_tag" value='<txp:smd_tag_name title="0" />' />
</txp:smd_tag_list>
<txp:smd_tag_list parent="" showall="1" wraptag="ul" break="" sublevel="all" >
<li<txp:if_variable name="current_tag" value='<txp:smd_tag_name title="0" />'> class="smd_tag_name_current"</txp:if_variable>><txp:smd_tag_name link="1" title="1" /></li>
</txp:smd_tag_list>
This will put out a list of all tags with the class smd_tag_name_current added to the li at the currently active tag list.
Last edited by saccade (2014-11-27 09:22:27)
Offline
#295 2015-04-16 08:51:36
Re: smd_tags: unlimited article, image, file and link taxonomy
Hi, maybe I wrong something but I use the Example 1: linked tag tree
and I have setting in preference Trigger(s) for tag lists: articles.
Now, in articles page, I have put this code:
<section class="wrapper">
<txp:smd_if_tag>
<txp:article limit="1" />
<txp:else />
<txp:article form="simple" />
</txp:smd_if_tag>
</section>
But independently of tag I send to this page it show always the same article. Why?
Offline
#296 2015-04-16 11:22:24
Re: smd_tags: unlimited article, image, file and link taxonomy
MarcoK wrote #290023:
independently of tag I send to this page it show always the same article. Why?
Because you’re not changing the context. Your <txp:article> tag is picking up the default Textpattern context based on the URL: in this case, your Section. So it’ll display the most recent article from the articles Section, as if the smd_tags weren’t there.
What I think you want to do is display a list of article that have the tag given in the URL, correct? In that case, you might try something like this:
<txp:smd_if_tag>
<txp:smd_tag_list wraptag="" break="">
<txp:smd_related_tags wraptag="ul" break="li" />
</txp:smd_tag_list>
<txp:else />
<txp:article form="simple" />
</txp:smd_if_tag>
So what that does is the following:
- Am I in a tag context?
- If so, make a list of tags from the URL (normally just the one) and set the context to that tag.
- Ask smd_related_tags to find other articles that match the tag.
- Display them as a list of links with their titles.
- Else: display the article / article list from the current Section as given in the URL.
You can use <txp:smd_related_tags> as a container to customise what you see in the article list.
btw, this method comes with a big caveat: ensure that your tag names never match the url-title of an article, because you’re (ab)using the articles Section for two purposes. Txp will always take precedence if there’s a clash. Or things might go a bit weird: I’m not entirely sure!
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
#297 2015-04-16 12:46:33
Re: smd_tags: unlimited article, image, file and link taxonomy
Many thanks Stef!!
Offline
#298 2015-06-04 12:26:12
- bojay
- Member

- Registered: 2010-04-13
- Posts: 11
Re: smd_tags: unlimited article, image, file and link taxonomy
Hi, I’m trying to get my head around structuring my tags.
And was wondering if some of you could share your thoughts.
I need my page to display a list of the four most relevant articles (for further reading) .. related to the article your current reading.
I have plenty of article and a lot of matching on a single-tags-level.
But if an article is tagged with “Fantasy” and “Frodo”, I’d love other article with both tags to be matched prior to something just matching “Fantasy”.
So if 10 article exist with both tags .. I need four random from those.
But then nothing from less-relevant matches eg. Only Fantasy and only Frodo.
And if only 2 other articles exist with both tags, I need them every time .. and then random from articles matching one of the other tags (preferably fixed)
At the moment my tags are without a parent, and all-on-the-same-level, but I can’t get my head around the structure and how to ask related_tags to match on more than one criteria.
Any thoughts are greatly appreciated :-)
Moderator’s annotation:
Edited to translate foreign language remainder.
– Uli –
Last edited by uli (2015-06-04 13:59:34)
Offline
#299 2015-06-04 13:58:58
Re: smd_tags: unlimited article, image, file and link taxonomy
bojay wrote #291301:
how to ask related_tags to match on more than one criteria.
That’s a very good point and something the plugin should be able to do. I don’t think it’s down to the hierarchy and structure of your tag lists per se, but something the plugin needs to offer. I was already partway there with jakob nudging me to add tag counts for the next version (although I think trying to match using tag_count doesn’t work properly so I need to look at that). Anyway, I just added a small mod to the next version to include the tag sums per content type. If you wouldn’t mind trying out the beta then you can do what I think you want like this:
<txp:smd_related_tags limit="4" sort="tag_sum desc" />
That should return only the articles that match any of the tags in the current article, and the more tags that match, the higher up the list the matched article is. If you also want to pimp newer stuff over older you can use sort="tag_sum desc, Posted desc".
If you’re comfortable compiling the plugin or have ied_plugin_composer available to install the new version directly from the template file, that’s grand. If you need a compiled version, drop me an email and I’ll send you one over.
Please let me know how well (and if!) it does what you want.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
#300 2015-06-04 14:04:07
Re: smd_tags: unlimited article, image, file and link taxonomy
Thinking out loud without having tried this, could you achieve this in a multi-step process?
1. first the multi-tag search but only use it to output id numbers separated by commas into a variable called for example two_tag_articles. A sample output could be “12,56,84” or whatever the id numbers of the matching articles happens to be.
2. A small bit of php along the lines of the following to count the number of matches and output that to a variable called two_tag_matches:
<txp:php>global $variable; $variable['two_tag_matches'] = count(explode(",", $variable['two_tag_articles']));</txp:php>
3. Test whether you have enough two_tag_matches, e.g. using smd_if.
3a) If you do, build a txp:article_custom statement with the id='<txp:variable name="two_tag_articles" />' limit="4" sort="rand()" you output your linked articles. That should give you a random list of four matching articles.
3b) If you don’t, do a similar exercise as 1) for your single tag variants, again outputting possible matches into a comma-separated list, or lists, e.g. one_tag_articles_a and one_tag_articles_b.
Then use something like the following to join the two lists of ids and shuffle them randomly, outputting them as a variable one_tag_articles.
<txp:php>global $variable; $tmp_list= explode(",", ($variable['one_tag_articles_a'] . "," . $variable['one_tag_articles_b']) ); $variable['one_tag_articles'] = implode("," , shuffle($tmp_list) );</txp:php>
Finally output your txp:article_custom statement with first the two_tag_articles, then the remaining one_tag_articles in the id attribute of your article_custom statement, e.g. id='<txp:variable name="two_tag_articles" />,<txp:variable name="one_tag_articles" />' limit="4".
It’s a bit convoluted – and only deals with your two tag plus two single tags situation – so maybe someone else has a better idea… [ EDIT: And Stef already does :-) ]
TXP Builders – finely-crafted code, design and txp
Offline