Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2019-10-11 10:04:57

antonh
New Member
Registered: 2019-10-11
Posts: 3

custom field value as a variable for link and category name

Hello.
I’m making a page that shows an article and outputs some values from custom fields (for example “artist”). I used the “<txp:custom_field name=‘artist’ />” tag to output the field content, and i would like that field content to be a link to another section that would take that fields value and display it as a category in the <article_custom category=”….” /> tag in that section. Its a kind of “more from this artist” link. I suppose that i would have to extract that value as a variable on the page from which I’m linking and pass it to the page that I’m linking to. Is there any plugin that does that, and what would be the right use of it for this example?

Offline

#2 2019-10-11 10:53:22

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 8,962
Website

Re: custom field value as a variable for link and category name

Hi and welcome to the forum.

You may be able to do this without any plugins. If you fashion your links so they end up at example.org/category/name-of-artist and have your articles categorised by artist in (for example) Category 1 then Textpattern can automatically show you all articles in that category on the landing page.

Doing it this way – making the category the artist name – you don’t need the custom field as you can display a list of all artists using the category_list tag and output the category name (a.k.a. the artist) on an individual artist page with a link to the category landing page mentioned above that contains all their work.

If you would like a hand setting this up or if that approach doesn’t suit your needs, please post here with some samples of what you’re trying to achieve and we’ll be able to assist you.


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 2019-10-11 11:12:53

antonh
New Member
Registered: 2019-10-11
Posts: 3

Re: custom field value as a variable for link and category name

Hi thank you very much for replying so quickly.
The example of work in progress of what I’m trying to do can be seen on: http://antonhorvatovic.com/artstock/

When you click on the artists work thumbnail you get to another page with the details of that work. The author name (as well as other infos as “pencil’ should lead to a generic page that lists the artworks but the link defines the category of articles on that page. I have been trying to achieve it by the tag <txp:variable name=“author”/> and repeating that tag inside the <txp:article_custom category=”<txp:variable name=‘author’/>” on the linked page – But I’m probably doing something wrong.
The accent is here on artworks and not the artists, its an artwork database, and artist name link just helps visualising all the other artworks by that artist if they exist in the database.
The Artworks are organised as articles with an article image, title and description contained in the body of the article. Custom fields contain all the informations displayed on the right from the image in the detail page.

Maybe it could be done in the simplier way but as I encounter this need for the first time after developing few websites in textpattern, I would like to learn something new as I see i could have a good use of it in the next projects. Any help would be appreciated, I’m sorry if I’m missing some basic programming syntax – I’m not really a coder but I’m always happy to learn more.

Last edited by antonh (2019-10-11 12:05:56)

Offline

#4 2019-10-11 12:09:12

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 3,662
Website

Re: custom field value as a variable for link and category name

It depends how much flexibility you need in displaying or filtering the artworks, but bloke’s suggestion is a great way of doing it with in-built methods because the txp:article tag will respond automatically to the category in the browser URL.

For example, if you make a parent category “artist” and sub-categories for each of your individual artists and a category “medium” with “pencil”, “painting”, “mixed media”, etc. as sub-categories, and then assign these to the category1 and category2 fields of your respective articles (one for each artwork) you automatically get:

  • example.org will show all the artworks
  • example.org/category/artist-name will show just the artworks by that artist
  • example.org/category/pencil will show just the pencil artworks

On your painting page use <txp:category1 link="1" title="1" /> (docs here) to create the link to the author and <txp:category2 link="1" title="1" /> to create the link to the medium, assuming you have consistently used Category 1 for the artist name and Category 2 for the medium in your articles.

As bloke says, you can use txp:category_list to create a list of artists (using the attribute parent="artists") or to group your gallery by artist, e.g.

<!-- a simple linked author list -->
<txp:category_list parent="artists" wraptag="ul" break="li">
    <txp:category link="1" title="1" />
</txp:category_list>

or if you want to show the paintings grouped by artist, you can do:

<!-- save all the artist categories in a variable -->
<txp:variable name="artist_categories" escape="trim">
    <txp:category_list parent="artists" break=","><txp:category /></txp:category_list>
</txp:variable>

<!-- create an article list sorted by artist interspersed with artists names -->
<txp:article_custom category='<txp:variable name="artist_categories" />' limit="999" sort="category1 desc, posted desc">
    <txp:if_different><txp:category1 title="1" wraptag="h3" /></txp:if_different>
    <figure>
        <txp:article_image />
        <figcaption><txp:title /></figcaption>
    </figure>
</txp:article_custom>

— — —

If you need to do something more complex and really want to use custom_fields rather than categories, you need three parts to achieve what you want:

  1. Generate a link using your custom field, e.g. http://example.org/artworks?artist=<txp:custom_field name="artist" /> on the individual painting page. BTW: I would suggest not using ‘author’ as that already exists in textpattern and you will want to avoid this clashing later in part 3.
  2. Read out the value for artist from the URL using something like rah_gps, for example: <txp:rah_gps name="artist" /> which will make a variable called artist out of the URL value.
  3. Use that variable together with article_custom to filter your output. If you’ve assigned custom fields to your articles, you need to do:
<!-- retrieve value from URL -->
<txp:rah_gps name="artist" />
<!-- if the artist value exists, e.g. is not empty -->
<txp:if_variable name="artist" value="" not>
    <!-- output all articles where the 'artist' custom-field is assigned to your artist -->
    <txp:article_custom artist='<txp:variable name="artist" />' />
<txp:else />
    <!-- show all articles -->
    <txp:article />
</txp:if_variable>

Note that you need to use single quotes for the attribute when using tags as values for tags.

EDIT: switched from adi_gps to rah_gps in the above code as the latter filters malicious URL input before using it to query the database and is thus safer. (adi_gps may have been updated since, I’m not sure).


TXP Builders – finely-crafted code, design and txp

Offline

#5 2019-10-11 12:34:45

antonh
New Member
Registered: 2019-10-11
Posts: 3

Re: custom field value as a variable for link and category name

Thank you so much for this great support! I’ll try both of the options and see what better suits the websites needs. You guys are great! Hope this post can be useful to others too.

Offline

#6 2019-10-11 12:51:13

etc
Developer
Registered: 2010-11-11
Posts: 3,530
Website

Re: custom field value as a variable for link and category name

To complete,

<txp:if_variable name="artist" value="" not>

can be shortened to

<txp:if_variable name="artist" value>

And in txp 4.7+ you can retrieve (sanitized) artist value natively with

<txp:page_url type="artist" />

This use-case also suggests that we might reduce

<txp:article_custom artist='<txp:page_url type="artist" />' />

to just

<txp:article_custom artist />

in txp 4.8.

Offline

#7 2019-10-11 12:55:55

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 3,662
Website

Re: custom field value as a variable for link and category name

Excellent tips Oleg. Must note these more consistently!

With the last two, what happens when there is no URL input for artist? Does article_custom simply ignore it? If so, that obviates the need for the if_variable as well (which I think you are suggesting).


TXP Builders – finely-crafted code, design and txp

Offline

#8 2019-10-11 13:20:41

etc
Developer
Registered: 2010-11-11
Posts: 3,530
Website

Re: custom field value as a variable for link and category name

jakob wrote #319648:

With the last two, what happens when there is no URL input for artist? Does article_custom simply ignore it? If so, that obviates the need for the if_variable as well (which I think you are suggesting).

That’s what I’d expect, but from the code it looks like it searches for empty artist. You can fix it with default attribute:

<txp:article_custom artist='<txp:page_url type="artist" default="%" />' />

But valueless artist (presently underemployed) could act this way.

Offline

#9 2019-10-11 13:50:19

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 3,662
Website

Re: custom field value as a variable for link and category name

etc wrote #319649:

… it looks like it searches for empty artist. You can fix it with default attribute:

<txp:article_custom artist='<txp:page_url type="artist" default="%" />' />...

Interesting! It got me thinking as to whether one could use this method to filter by multiple criteria, i.e. by placing several custom-field filter attributes in an article_custom tag and letting it filter by whatever combination appears in the URL, e.g.

<txp:article_custom artist='<txp:page_url type="artist" default="%" />'
                    medium='<txp:page_url type="medium" default="%" />'
                    date='<txp:page_url type="date" default="%" />'
                    availability='<txp:page_url type="availability" default="%" />'
                    />

This would mean that ?artist=joe-bloggs&medium=drawings&availability=for-sale would spit out just the unsold drawings by joe bloggs.

In the past I’ve turned to smd_query to build more complex database queries like this, but this would be simpler and quite powerful… In the very old days, glz_custom_fields had a front-end tag that replaced article_custom and made it possible to search by multiple criteria given in custom fields, but Gerhard removed it in later versions.

But valueless artist (presently underemployed) could act this way.

I’m intrigued by this too! Can you expand on this?


TXP Builders – finely-crafted code, design and txp

Offline

#10 2019-10-11 14:09:51

etc
Developer
Registered: 2010-11-11
Posts: 3,530
Website

Re: custom field value as a variable for link and category name

jakob wrote #319651:

This would mean that ?artist=joe-bloggs&medium=drawings&availability=for-sale would spit out just the unsold drawings by joe bloggs.

It does mean this, indeed, even in <txp:article />.

I’m intrigued by this too! Can you expand on this?

The idea is that

<txp:article_custom artist />

would set artist filter

  • to the containing article’s artist cf value (if not empty) when used inside some article context;
  • to URL artist parameter (if set) outside article context;
  • ignored if none of above is provided.

Offline

Board footer

Powered by FluxBB