Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-01-16 23:22:57

alannie
Member
From: Minnesota, USA
Registered: 2005-09-15
Posts: 150

How do I list ONLY articles that are in two specified categories?

I would like to generate a list of articles that are in category “foo” AND (not OR) category “bar”. Not all articles from foo plus all articles from bar, but only the ones that are in both.

How do I do this without requiring that one always be put into Category1 and the other Category2?

I tried smd_if but it doesn’t seem to like multiple instances of the same field.

Offline

#2 2008-01-16 23:33:24

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: How do I list ONLY articles that are in two specified categories?

Not tested, but you could give it a try.

Article form:

<txp:if_article_category name="foo">
<txp:article_custom category="bar" form="other_article_form" />
</txp:if_article_category>

Offline

#3 2008-01-16 23:37:09

alannie
Member
From: Minnesota, USA
Registered: 2005-09-15
Posts: 150

Re: How do I list ONLY articles that are in two specified categories?

Wouldn’t that just result in a list of all “bar” articles, though? article_custom is not context-sensitive.

Offline

#4 2008-01-16 23:40:48

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: How do I list ONLY articles that are in two specified categories?

I’m not sure. I assumed it works like this: first all ‘foo’ articles are selected, and from those the ‘bar’ articles are selected.

Offline

#5 2008-01-16 23:50:06

redbot
Plugin Author
Registered: 2006-02-14
Posts: 1,410

Re: How do I list ONLY articles that are in two specified categories?

Maybe I’m wrong but I would use:

<txp:article_custom category="bar" form="your_form" />

in your page template and:

<txp:if_article_category name="foo">
<txp:title />
</txp:if_article_category>

in your article form

Last edited by redbot (2008-01-17 00:18:39)

Offline

#6 2008-01-16 23:51:44

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: How do I list ONLY articles that are in two specified categories?

That may even be better!

Offline

#7 2008-01-17 00:11:03

alannie
Member
From: Minnesota, USA
Registered: 2005-09-15
Posts: 150

Re: How do I list ONLY articles that are in two specified categories?

I just realized I neglected to mention part of the dilemma I’m facing. In addition to 1) displaying a list of foo+bar articles, I also need 2) for this list to only display if the current article is itself a foo+bar article.

Redbot, your suggestion does work for #1 but #2 is stumping me. Here’s the temporary workaround I have:

In page template:

<txp:smd_if field="category1, category2"
operator="eq, eq"
value="foo, bar">
<txp:article_custom form="list-bar" category="foo" />
</txp:smd_if>

In article form list-bar:

<txp:if_article_category name="bar">
<p><txp:permlink><txp:title /></txp:permlink></p>
</txp:if_article_category>

This works for now but I will probably need to add more than just two categories, which is why I want to get away from category1 and category2 if possible.

Much appreciate the ideas thus far, keep them coming, maybe we’ll hit upon something.

Offline

#8 2008-01-17 01:39:13

redbot
Plugin Author
Registered: 2006-02-14
Posts: 1,410

Re: How do I list ONLY articles that are in two specified categories?

alannie wrote

but I will probably need to add more than just two categories, which is why I want to get away from category1 and category2 if possible

Ehm sorry I don’t know if understood what you mean exactly,
if you mean you want a list of articles belonging to category_x + category_y + category_z you could try with this code (I’m not sure it will work, actually I never used smd_if). This is based on the assumption that in txp you can’t nest the same conditional tag.

<txp:smd_if field="category"
operator="eq"
value="category_x">
<txp:if_category name="category_y">
<txp:article_custom form="list-bar" />
</txp:if_category>
</txp:smd_if>

and in your article form:

<txp:if_article_category name="category_z">
<p><txp:permlink><txp:title /></txp:permlink></p>
</txp:if_article_category>

Offline

#9 2008-01-17 18:16:18

alannie
Member
From: Minnesota, USA
Registered: 2005-09-15
Posts: 150

Re: How do I list ONLY articles that are in two specified categories?

Thanks, redbot. When I mentioned adding more than just two categories, I was referring to the default “Category 1” and “Category 2” pulldown menus in the TXP admin when you’re editing an article in the Write tab. Initially I was thinking I might need to label an article with three or more categories, so a solution that required specifying category1 and category2 as attributes would not have worked.

I’ve given up on the foo+bar approach and will just be creating explicit sub-categories for each group. A bit more tedious but should work.

I tried out your code example, but the pages where I need this list to show up are not article list pages, so the if_category tag does not work. Thanks though for helping me think through the problem and pinpoint my options.

Offline

#10 2008-01-17 18:20:25

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

Re: How do I list ONLY articles that are in two specified categories?

alannie wrote:

I tried smd_if but it doesn’t seem to like multiple instances of the same field.

Oops, when you say “doesn’t seem to like”, does it generate an error or just not work as expected? Can you send me the tag you tried and the output of smd_if when you add debug="1" to your tag please?

But as far as testing cat1 and cat2 together, you’re right, that syntax is tricky for the current version. Would it help if I added the ability to specify either:

a) <txp:smd_if field="categories,categories" operator="eq,eq" value="foo,bar">

to simultaneously check all categories for both values. [ equiv. logic is: (if global_category EQ foo OR category1 EQ foo OR category2 EQ foo) AND (if global_category EQ bar OR category1 EQ bar OR category2 EQ bar) ]

Or b) <txp:smd_if field="category1, category2" operator="eq, eq" value="foo|bar, foo|bar">

to specify multiple values for a single field. In this case ‘|’ means ‘or’; I could come up with an ‘and’ character too. [ equiv. logic is: (if category1 EQ foo OR category1 EQ bar) AND (if category2 EQ foo OR category2 EQ bar) ]

Not sure if the combination works c) <txp:smd_if field="categories" operator="eq" value="foo|bar">

[ equiv. logic is: (if global_category EQ foo OR category1 EQ foo OR category2 EQ foo OR global_category EQ bar OR category1 EQ bar OR category2 EQ bar) … ummmm, I think! :-) ]

Not sure if I can pull it off, but if you think it’ll be any use I can certainly try for you! Any preference? Can you think of a better syntax?

Last edited by Bloke (2008-01-17 18:21:24)


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

Online

#11 2008-01-17 19:00:52

alannie
Member
From: Minnesota, USA
Registered: 2005-09-15
Posts: 150

Re: How do I list ONLY articles that are in two specified categories?

Bloke wrote:

Oops, when you say “doesn’t seem to like”, does it generate an error or just not work as expected? Can you send me the tag you tried and the output of smd_if when you add debug=“1” to your tag please?

It wasn’t returning any results. But when I turned on debugging like you suggested, I realized that it wasn’t the multiple instances of a field that was tripping it up. Even a single instance returns “false”.

I was trying to use “category” to check an article’s category but now I’m wondering if that actually checks only whether the page is a category article list? Let me know if I should be using a different field to check whether an article is in a specific category. Below is the code I initially tried out.

Page template code

<txp:smd_if field="category, category"
operator="eq, eq"
value="foo, bar"
debug="1">
<txp:article_custom form="list-bar" category="foo" />
</txp:smd_if>

Article form list-bar

<txp:if_article_category name="bar">
<p><txp:permlink><txp:title /></txp:permlink></p>
</txp:if_article_category>

Debug results

TEST 1
array (
  0 => 'category',
)
array (
  0 => 'eq',
)
array (
  0 => 'foo',
)
$out[] = (strtolower($pretext["c"]) === strtolower('foo')) ? 'true' : 'false';
TEST 2
array (
  0 => 'category',
)
array (
  0 => 'eq',
)
array (
  0 => 'bar',
)
$out[] = (strtolower($pretext["c"]) === strtolower('bar')) ? 'true' : 'false';
RESULT:
array (
  0 => 'false',
  1 => 'false',
)

Offline

#12 2008-01-17 19:09:57

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

Re: How do I list ONLY articles that are in two specified categories?

Yes, category is the global category. I’ll try and make that clearer in the help next revision. Using field="category1" (or category2) should match the relevant fields in an article. I still don’t think it’ll help much because you can only use one type of “logic” per smd_if call, and you can’t nest them :-(

I do have plans to expand the logic (see these posts)


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

Online

Board footer

Powered by FluxBB