Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2022-01-23 15:29:12

draganbabic
Member
From: Novi Sad, Serbia
Registered: 2006-09-27
Posts: 118
Website

Need to display categories to which the article belongs to

I have an article form, and I would like to output the category 1 and category 2 as a simple comma-separated line of text. For some reason I can’t seem to do this as my TXP chops are really rusty.

txp:category and txp:category_list keep returning all categories present in the system.

Can someone point me to the right direction? Thanks!

Offline

#2 2022-01-23 15:54:52

Dragondz
Moderator
From: Algérie
Registered: 2005-06-12
Posts: 1,529
Website GitHub Twitter

Re: Need to display categories to which the article belongs to

Hi

Can you post your article form ?

Cheers.

Offline

#3 2022-01-23 16:38:59

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

Re: Need to display categories to which the article belongs to

Hi Dragan, nice to see you still have the odd Textpattern site at super-awesome :-)

For an article, you need txp:category1 and txp:category2. You can simply output them one after the other like this:

<txp:category1 title /><txp:category2 title wraptag=", <+>" />

(the wraptag construction in this form is comparatively new, where <+> represents the tag output. Add the link attribute if you want them to link to a “view by category” page)

Or you can do what Phil has done in the default article template which is more robust as it can cater for category2 being set but category1 unset:

<txp:category_list categories='<txp:category1 />,<txp:category2 />' children="0" break=", " trim>
    <txp:category title link />
</txp:category_list>

(this will only output one category if only one is set)

There are more variations, e.g. as a ul-list with the comma added using css or using txp:evaluate to test if one or the other (or both) exists …


TXP Builders – finely-crafted code, design and txp

Offline

#4 2022-01-23 16:39:48

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,011
Website GitHub Mastodon Twitter

Re: Need to display categories to which the article belongs to

This is from the default article form:

        <txp:if_article_category>
            <br>
            <strong>
                <txp:text item="categories" />
            </strong>
            <span itemprop="keywords">
                <txp:category_list categories='<txp:category1 />,<txp:category2 />' children="0" break=", " trim>
                    <txp:category title link />
                </txp:category_list>
            </span>
        </txp:if_article_category>

Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#5 2022-01-23 16:41:46

draganbabic
Member
From: Novi Sad, Serbia
Registered: 2006-09-27
Posts: 118
Website

Re: Need to display categories to which the article belongs to

Here it is:

<title><txp:title no_widow="0" /></title>
<author><txp:author /></author>
<posted><txp:posted format="%Y-%m-%dT%H:%M:%S%z" /></posted>
<url><txp:article_url_title /></url>
<id><txp:article_id /></id>
<excerpt><![CDATA[<txp:excerpt />]]></excerpt>
<body><![CDATA[<txp:body />]]></body> 

I am using this form to output all articles from a section through rah_external_output plugin to an XML file.

And the misc form that rah_external_output is using is this:

<?xml version="1.0"?>
<sections>
  <txp:section_list sections="vezbe" break="section" class="">
    <txp:section title="1" wraptag="name" />
    <txp:section wraptag="url" />
    <articles>
      <article class="sticky">
        <txp:article_custom section='<txp:section />' status="sticky" limit="1">
          <txp:output_form form="article_export" />
        </txp:article_custom>
      </article>
      <txp:article_custom section='<txp:section />' limit="9999" break="article">
        <txp:output_form form="article_export" />
      </txp:article_custom>
    </articles>
  </txp:section_list>
  <txp:section_list sections="weblog" break="section" class="">
    <txp:section title="1" wraptag="name" />
    <txp:section wraptag="url" />
    <txp:article_custom section='<txp:section />' limit="999" wraptag="articles" break="article">
      <txp:output_form form="article_export" />
    </txp:article_custom>
  </txp:section_list>
</sections>

Last edited by draganbabic (2022-01-23 16:42:49)

Offline

#6 2022-01-23 16:49:25

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

Re: Need to display categories to which the article belongs to

Depending on whether you want both categories in a single xml line:

<categories><![CDATA[<txp:category_list categories='<txp:category1 />,<txp:category2 />' children="0" break=", " trim><txp:category /></txp:category_list>]]></categories>

or each on an own line:

<txp:category_list categories='<txp:category1 />,<txp:category2 />' children="0" break=", " trim>
    <category><![CDATA[<txp:category />]]></category>
</txp:category_list>

Wrap either with <txp:if_article_category> … </txp:if_article_category> if you want no output when there are no categories assigned.


TXP Builders – finely-crafted code, design and txp

Offline

#7 2022-01-23 17:01:59

draganbabic
Member
From: Novi Sad, Serbia
Registered: 2006-09-27
Posts: 118
Website

Re: Need to display categories to which the article belongs to

Thank you jakob! That did the trick.

Offline

Board footer

Powered by FluxBB