Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2005-11-01 01:29:52

squaredeye
Member
From: Greenville, SC
Registered: 2005-07-31
Posts: 1,495
Website

[howto] how to use if_custom_field

Not a hard one really, but I think, really useful

If the custom field is empty. ie: if it wasn’t filled in at the writing of the article, then it won’t be present in the display. I use this for displaying information about artworks at artiswork.org for instance.

<p>
<txp:if_custom_field name="materials">
		<strong>materials: </strong>
		<txp:custom_field name="materials" /><br/>
</txp:if_custom_field>

<txp:if_custom_field name="size if applicable">
		<strong>size: </strong>
		<txp:custom_field name="size if applicable" /><br/>
</txp:if_custom_field>

<txp:if_custom_field name="year/date">
		<strong>date: </strong>
		<txp:custom_field name="year/date" /><br/>
</txp:if_custom_field>
</p>

If this is a little more geared toward the textbook (basic use) sorry :)

Matthew

Last edited by ma_smith (2006-01-07 22:33:35)


Offline

#2 2005-11-01 03:32:33

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

Re: [howto] how to use if_custom_field

<txp:if_custom_field name="materials"> <strong>date: </strong> <txp:custom_field name="year/date" /><br/>
</txp:if_custom_field>

I dont know if the mistake above was intentional or it was part of the technique suggested by ma_smith.

In the above chunk, the value of “name” attribute isnt the same for txp:if_custom_field and for txp:custom_field.
For the first (if_custom_field), the value is “materials” and for the second one (custom_field) the value is “year/date”.

This can be seen as a mistake or if you are smart enough, as a powerful technique: using the custom_fields to trigger code.
In this case, if there is any value (= it’s not empty) in “materials” custom field, it will return the value inside the “year/date” custom field.

Of course, it seems a bit difficult to find situations where this can be useful. Or maybe not.
I have no doubt that someone in this community will find a great example and then will come back here and post it in this thread.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#3 2005-11-01 03:39:55

squaredeye
Member
From: Greenville, SC
Registered: 2005-07-31
Posts: 1,495
Website

Re: [howto] how to use if_custom_field

Oops. Thanks Maniqui. Good Catch. I would have been wondering about that on my site as well :) Guess that’s why they call you a moderator.

Cheers,

Matthew

EDIT:

Oops AGAIN! I should have read your post better. I should have claimed that yes, indeed I am a genius coder and you all should hang on my every example, ha ha ah ha ah ha. (evil laugh). Oh well. :)

Last edited by ma_smith (2005-11-01 03:42:50)


Offline

#4 2005-11-01 20:36:19

Inspired
Plugin Author
From: Kiev, Ukraine
Registered: 2005-01-28
Posts: 87
Website

Re: [howto] how to use if_custom_field

A tip of how this technique could be useful for multilingual sites.

Let’s assume you want to have two sets of pages which would be crosslinked with links like “see this page in [another language]”. Set custom fields for the languages you want to support, name them, for example eng and ukr (for English and Ukrainian).

Write articles for those languages and assign them links of their sibling articles on the other language, say, you have an article in English with permanent link “/about/my-dog” and you have the same article in Ukrainian with permanent link “/about/my-dog-ukr”, then set “/about/my-dog” to the custom field “eng” in the article on Ukrainian and “/about/my-dog-ukr” to the custom field “ukr” in the article on English.

Then in the article form you use write a code similar to the following:

<txp:if_custom_field name="eng">
<a href="<txp:custom_field name="eng" />">this page in english</a>
</txp:if_custom_field>

<txp:if_custom_field name="ukr">
<a href="<txp:custom_field name="ukr" />">this page in ukrainian</a>
</txp:if_custom_field>
<br />

That’s it! When it’s English link to Ukrainian article will be shown up and vice versa.
It’s just a basic example to give you an idea, hope it helps.

Yura


Plugin Composer — admin plugin to write your own plugins

Offline

#5 2005-12-01 00:14:32

Destry
Member
From: Haut-Rhin
Registered: 2004-08-04
Posts: 4,909
Website

Re: [howto] how to use if_custom_field

Yura, not only is that an innovating use of conditionals, but it’s probably the most elegant solution to the multilingual site problem yet. Being that I don’t expect to translate every article I write, this is likely going to be perfect for my needs. As soon as I find the time, I’ll try it.

Offline

#6 2005-12-01 03:33:53

NXArmada
Member
From: Sevierville, TN
Registered: 2005-09-13
Posts: 82

Re: [howto] how to use if_custom_field

I use this tag alot on the company that I work for, on there webiste. Right now that site is in beta and the site should launch in Jan. and it well be 100% controled by TXP.

I use this tag to auto-generate the Add to Cart buttons used in the shopping cart system on the site. I use all 10 fields for the buttons and use the if tags to only display the buttons for the filled in feilds. Its awsome.


Ryan

Offline

#7 2005-12-09 18:57:28

Jeff_K
Archived Plugin Author
From: Vancouver, British Columbia
Registered: 2005-08-19
Posts: 202
Website

Re: [howto] how to use if_custom_field

Any idea of how to output information if a custom field is not set to a certian value i.e., something like <txp:if_custom_field name="colour" value!="red">blue</txp:if_custom_field>?

Last edited by Jeff_K (2005-12-09 20:01:49)

Offline

#8 2005-12-10 00:39:50

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

Re: [howto] how to use if_custom_field

Have you tested using txp:else?

<txp:if_custom_field name="colour" value="red">
...red...
<txp:else />
...blue...
</txp:if_custom_field>

La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#9 2005-12-10 08:04:18

Jeff_K
Archived Plugin Author
From: Vancouver, British Columbia
Registered: 2005-08-19
Posts: 202
Website

Re: [howto] how to use if_custom_field

oh duh. just leave the if statement blank and supply an else statement. makes sense to me.

Offline

#10 2010-06-09 17:37:52

Vide-Dressing
Member
Registered: 2010-06-07
Posts: 31

Re: [howto] how to use if_custom_field

custom_fields are the most powerful features in textpattern.
This howto was published in 2005, and still usefull till now days. Thank you squaredeye

Offline

Board footer

Powered by FluxBB