Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2018-11-10 13:14:41

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

article_custom from both categories (AND)

Forgive me for being dim, but I’m having a moment. How can I use <txp:article_custom> to output articles that match BOTH categories when I supply two:

<txp:article_custom section="stock" limit="999" category='location, type'>
   ...
</txp:article_custom>

I’m setting category1 = ‘location’ and category2=‘type’ in the articles and I’m trying to output a series of tables that contain articles from each location (h2) and type (h3)- it’s a stocklist. I tried to do it in one giant article_custom call sorted by category2,category1 and then use if_different to show the headings but things fall apart when constructing the end table/tbody in the loop.

So far the closest I can get is this:

<txp:category_list parent="location" sort="title desc" exclude="location">
    <txp:variable name="n_location"><txp:category /></txp:variable>
    <h2><txp:category title /></h2>
    <txp:category_list parent="type" sort="title desc" exclude="type">
        <txp:variable name="n_type"><txp:category /></txp:variable>
        <h3><txp:category title /></h3>
        <table>
            <thead>
                <tr>
                <th>Serial number</th>
                <th>Year</th>
                <th>Condition</th>
                <th>Price</th>
                </tr>
            </thead>
            <tbody>
            <txp:article_custom section="stock" limit="999" category='<txp:variable name="n_location" />, <txp:variable name="n_type" />'>
            <tr>
                <td><txp:custom_field name="Serial number" /></td>
                <td><txp:custom_field name="Year" /></td>
                <td><txp:custom_field name="Condition" /></td>
                <td><txp:custom_field name="Price" /></td>
            </tr>
            </txp:article_custom>
            </tbody>
        </table>
    </txp:category_list>
  </txp:category_list>

But that’s outputting the same stuff in each group because it’s matching category1 OR category2. Anyone any ideas how I can approach this? I don’t mind any solution but I’d rather not have to revert to PHP unless I have to.

It’d also be great to output the custom field names instead of hard-coding the headings but I can live with it for now.

Thank you in advance.

(Txp 4.7.1, btw)


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

#2 2018-11-10 13:21:49

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: article_custom from both categories (AND)

Dunno if it helps here, but you can set match="Category1" in <txp:article_custom /> to match only… suspense… Category1.

Offline

#3 2018-11-10 13:28:16

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

Re: article_custom from both categories (AND)

Thanks, yeah. Not sure it does actually help here because I don’t really want to nest article_custom calls. But I guess I could.

Another addendum to this is that I’d like to pull out only articles where custom_3 (price) does NOT match the word SOLD. So this stocklist only shows for-sale items in the shop, but if you visit the section it can still show the products sold as well.

I can’t figure out how to say ‘custom_3 not SOLD’. I’ve tried the usual suspects but NOT seems to negate everything including the section and category attributes in the tag!

Ideas welcome.


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

#4 2018-11-10 13:32:03

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: article_custom from both categories (AND)

Have you tried

<txp:article_custom price="SOLD" exclude="price" />

Offline

#5 2018-11-10 13:43:45

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

Re: article_custom from both categories (AND)

etc wrote #315148:

<txp:article_custom custom3="SOLD" exclude="custom3" />...

Good idea. Never thought of trying that. Sadly it didn’t seem to work as I still get SOLD articles in the list:

<txp:article_custom section="stock" limit="999" category='<txp:variable name="n_location" />, <txp:variable name="n_type" />' sort="custom_2 asc" custom3="SOLD" exclude="custom3">
   ...
</txp:article_custom>

Hmmm…


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 2018-11-10 13:49:41

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: article_custom from both categories (AND)

Bloke wrote #315147:

I don’t really want to nest article_custom calls.

You wouldn’t nest but duplicate, though it’s not ideal:

<txp:variable name="ids">
    <txp:article_custom section="stock" limit="999" category='<txp:variable name="n_location" />' match="Category1" break=",">
        <txp:article_id />
    </txp:article_custom>
</txp:variable>

<txp:article_custom id='<txp:variable name="ids" />' section="stock" limit="999" category='<txp:variable name="n_type" />' match="Category2">
    <tr>
        <td><txp:custom_field name="Serial number" /></td>
        <td><txp:custom_field name="Year" /></td>
        <td><txp:custom_field name="Condition" /></td>
        <td><txp:custom_field name="Price" /></td>
    </tr>
</txp:article_custom>

The variable definition must be outside the second category_list, of course.

Offline

#7 2018-11-10 13:50:47

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: article_custom from both categories (AND)

Bloke wrote #315149:

Good idea. Never thought of trying that. Sadly it didn’t seem to work as I still get SOLD articles in the list:

Sorry, it must be the custom field name, e.g. price if you have named it so:

<txp:article_custom price="SOLD" exclude="price" />

Offline

#8 2018-11-10 13:54:09

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

Re: article_custom from both categories (AND)

etc wrote #315151:

Sorry, it must be the custom field name

D’oh, of course. Genius. That’s one issue solved. I’ll remember that trick, thank you.

Now just the dual category somehow.


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 2018-11-10 13:56:21

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: article_custom from both categories (AND)

Independently, it would be easy to replace OR with AND when needed, we just need a syntax:

match="Category1 Category2", match="Category1+Category2", match="Category1&Category2", ...?

Offline

#10 2018-11-10 13:58:50

etc
Developer
Registered: 2010-11-11
Posts: 5,028
Website GitHub

Re: article_custom from both categories (AND)

Bloke wrote #315152:

Now just the dual category somehow.

I think this could work. It adds one db query per location, but the query inside the second category_list should use ID index and be fast.

Offline

#11 2018-11-10 14:22:03

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

Re: article_custom from both categories (AND)

etc wrote #315154:

I think this could work.

Ah sorry, didn’t spot that post. Yes, that works perfectly well, thank you. And doesn’t seem that resource hungry either. I added a little defensive if_variable goodness around the second category_list in case there’s no stock at any given location. Thank you so much, Oleg. Saved my sanity!

Independently, it would be easy to replace OR with AND when needed, we just need a syntax

I like that idea. As you say, the trick is coming up with a suitable syntax. A ‘plus’ for ‘OR’ might be a little counter intuitive as it might imply ‘add’ or ‘both’ to most.


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

#12 2018-11-10 16:47:09

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

Re: article_custom from both categories (AND)

As a little further addendum I’d like to display how many articles are in the stock section that are not SOLD and show it in the article title of the stocklist.

Presumably with some judicial use of escape magic in 4.7, I can add <txp:php> safe_count(...);</txp:php> in the article title, but so far it’s resisting my advances. Any clues how I can get this value out and displayed on the page where the <txp:title/> tag is?


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

Board footer

Powered by FluxBB