Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-08-28 13:10:45

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

How can I list articles related by keyword?

Textpattern has made major strides forward this year. I’m wondering if it’s possible to generate a list of articles— ideally using TXP’s native article or article_custom tags —related by keyword to the current article, and without using any orphaned plugins.

The first challenge is to be able to say OR in a keywords relation: right now keywords='<txp:keywords/>' returns only articles that match ALL the keywords associated with an article. For this, we would need to match any individual keyword.

The next challenge is to be able to iterate over the distinct keywords— if that’s possible, the rest could be handled with txp:if_different. Seems like txp:variable would come in pretty handy too, but I’m not sure how it would all hang together.

Has anyone mastered this task? Would you be willing to post a code snippet? Any alternative ways of thinking about this?

Offline

#2 2009-08-28 13:15:36

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,454
Website GitHub

Re: How can I list articles related by keyword?

johnstephens wrote:

The next challenge is to be able to iterate over the distinct keywords

Would smd_each help?


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

#3 2009-08-28 13:22:23

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: How can I list articles related by keyword?

Wow, very fast— and smd_each looks promising! I’ll try it out. Thanks!

Before you posted, I thought about how valuable a txp:array tag would be for this case, and then I found this. I never saw a commit that mentioned this— I wonder if it’s been slated.

Thank you again, sir! I hope you’re well!

Offline

#4 2009-08-28 13:38:12

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

Re: How can I list articles related by keyword?

and then I found this. I never saw a commit that mentioned this [test for multiple values in if_variable] — I wonder if it’s been slated.

I do hope not. As you saw, I queried this myself just a few days ago.

In the end I constructed a roundabout method with nested if_variables and testing for the not needed case and using the else part. And that only worked because I had one single exception. It would be much easier and cleaner to have added two commas and two words :-)


TXP Builders – finely-crafted code, design and txp

Offline

#5 2009-08-28 13:40:49

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,454
Website GitHub

Re: How can I list articles related by keyword?

johnstephens wrote:

smd_each looks promising

No probs. Hope it’s useful. If you want any tips ‘n’ stuff just give me a shout as I’ve played with the plugin a bit recently and learnt some cool stuff.

I never saw a commit that mentioned this— I wonder if it’s been slated.

I don’t think it got past that single post unfortunately. I guess if it’s raised on the Issues tracker as a feature it might get some more visibility :-)


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

#6 2009-08-28 15:31:03

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: How can I list articles related by keyword?

Okay, I’m almost there! smd_each was exactly the right tool for this. Only one bit of confusion.

I want to output a list of links to articles in another section that match by keyword— like so:

<ul>
<txp:smd_each type="field" include="keywords">
	<txp:article_custom section="other-section" keywords="{var_value}" break="li"><a href="<txp:permlink/>"><txp:title/></a></txp:article_custom>
</txp:smd_each>
</ul>

The above code works perfectly.

I also want to add a list item for each keyword that does not have an article in the other section, following the linked list items— like this:

  • Link to article for keyword 1
  • Link to article for keyword 2
  • keyword 3
  • keyword 4
  • etc…

I tried filtering the output using a txp:variable, but it didn’t work. Finally I entered this, without a conditional, to see the output:

<ul>
<txp:smd_each type="field" include="keywords">
	<txp:article_custom section="other-section" keywords="{var_value}" break="li"><a href="<txp:permlink/>"><txp:title/></a></txp:article_custom>
	<li>{var_value}</li>
</txp:smd_each>
</ul>

What I expected to see would be this:

  • Link to article for keyword 1
  • keyword 1
  • Link to article for keyword 2
  • keyword 2
  • keyword 3
  • keyword 4
  • etc…

But what I got was this:

  • Link to article for keyword 1
  • Link to article for keyword 2
  • keyword 1,keyword 2,keyword 3,keyword 4,etc…

I thought it would output a new li for each unique keyword, but instead it generated the same content as <txp:keywords/>.

Is there a way to get this to iterate though the keywords and output the content as in the first sample above?

P.S. After using smd_each, I’m not sure a Textpattern tag would be needed— it’s an awesome plugin, I just haven’t completely figured it out. Thanks again!

Offline

#7 2009-08-28 15:39:52

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: How can I list articles related by keyword?

jakob wrote:

“I wonder if [testing for multiple values in if_variable] has been slated.”

I do hope not. As you saw, I queried this myself just a few days ago.

In the end I constructed a roundabout method with nested if_variables and testing for the not needed case and using the else part. And that only worked because I had one single exception. It would be much easier and cleaner to have added two commas and two words :-)

It sounds like you would prefer to use a txp:variable or txp:array tag. Are you saying you hope not because your workaround is better, or you don’t want to revise it, or something else?

Offline

#8 2009-08-28 15:54:35

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,454
Website GitHub

Re: How can I list articles related by keyword?

johnstephens wrote:

I also want to add a list item for each keyword that does not have an article in the other section, following the linked list items— like this:

Right, that’s tricky to do in a single smd_each because the container is executed for every keyword in the current list. So what you’re seeing is the result of smd_each doing this:

  1. Pass keyword 1 to article_custom. If it matches something, the article_custom container is executed and a list is produced
  2. Output keyword 1 in li tags
  3. Pass keyword 2 to article_custom. If it matches something, the article_custom container is executed and a list is produced
  4. Output keyword 2 in li tags

What you need to do (in logic terms) is:

<smd_each>
  if article_custom call returns something
    // do nothing because you've already output the list as part of the article custom container
  else
    // show raw keyword
  end_if
</smd_each>

So your article custom portion works as now, but it returns its output in such a way that it can be tested by chh_if_data or assigned to a variable to be tested via if_variable, etc. That’s no mean feat and will require some skulduggery, probably involving the <txp:variable name="has_content" value='<txp:article_custom ... />' /> dance.

Last edited by Bloke (2009-08-28 15:55:22)


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

#9 2009-08-28 16:10:46

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: How can I list articles related by keyword?

So what you’re seeing is the result of smd_each doing this…

That’s what I expected to see, but what I got was this:

  1. Pass keyword 1 to article_custom. If it matches something, the article_custom container is executed and a list is produced.
  2. Pass keyword 2 to article_custom. If it matches something, the article_custom container is executed and a list is produced
  3. Output all keywords as a single string in a single pair of li tags.

I initially tested it with <txp:variable name="has_content" value='<txp:article_custom ... />' />-based conditionals, but in that case the only output I got was the lists produced by the article_custom tag, and the raw keywords never got listed. That’s why I tested it without the conditional (using the second code-block above), and noticed that the second {var_value} was outputting the entire string as one list item.

Offline

#10 2009-08-28 16:18:44

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

Re: How can I list articles related by keyword?

Are you saying you hope not because your workaround is better, or you don’t want to revise it, or something else?

Er… What I meant to say is that I wish one could do:

<txp:if_variable name="foo" value="apple,pear,orange">

At present to achieve the same you have to nest the else-cases of the non-applicable values:

<txp:if_variable name="foo" value="strawberry">
<txp:else />
   <txp:if_variable name="foo" value="cherry">
   <txp:else />
   <!-- apple, pears and oranges here -->
   </txp:if_variable>
</txp:if_variable>

and it gets more complicated if you have a large number of variables. It’s wordy and not logical. Imagine reading someone else’s code – you’d never guess that all they want is apples, pears and oranges. Or is there a simpler way with the in-built tags?

(ps: sorry for sidetracking your thread)

Last edited by jakob (2009-08-28 16:21:06)


TXP Builders – finely-crafted code, design and txp

Offline

#11 2009-08-28 16:31:00

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: How can I list articles related by keyword?

Even this code outputs the keywords array as a single string, contained in a single li pair. What am I doing wrong?

<txp:smd_each type="field" include="keywords">
	<li>{var_value}</li>
</txp:smd_each>

Result:

<li>keyword 1,keyword 2,keyword 3,etc...</li>

Last edited by johnstephens (2009-08-28 16:31:18)

Offline

#12 2009-08-28 16:57:02

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

Re: How can I list articles related by keyword?

It seems like the collate attribute is being turned on by default (which, it shouldn’t happen, me thinks).


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB